blob: 7e4be5baac94dfc0f1ba5b81b152cbeb2faefc36 [file] [log] [blame]
Tim Peters2a799bf2002-12-16 20:18:38 +00001/* C implementation for the date/time type documented at
2 * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage
3 */
4
5#include "Python.h"
Tim Peters2a799bf2002-12-16 20:18:38 +00006#include "structmember.h"
7
8#include <time.h>
9
Victor Stinner09e5cf22015-03-30 00:09:18 +020010#ifdef MS_WINDOWS
11# include <winsock2.h> /* struct timeval */
12#endif
13
Tim Peters9ddf40b2004-06-20 22:41:32 +000014/* Differentiate between building the core module and building extension
15 * modules.
16 */
Guido van Rossum360e4b82007-05-14 22:51:27 +000017#ifndef Py_BUILD_CORE
Tim Peters9ddf40b2004-06-20 22:41:32 +000018#define Py_BUILD_CORE
Guido van Rossum360e4b82007-05-14 22:51:27 +000019#endif
Tim Peters2a799bf2002-12-16 20:18:38 +000020#include "datetime.h"
Tim Peters9ddf40b2004-06-20 22:41:32 +000021#undef Py_BUILD_CORE
Tim Peters2a799bf2002-12-16 20:18:38 +000022
Larry Hastings61272b72014-01-07 12:41:53 -080023/*[clinic input]
Larry Hastings44e2eaa2013-11-23 15:37:55 -080024module datetime
Larry Hastingsc2047262014-01-25 20:43:29 -080025class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType"
Larry Hastings61272b72014-01-07 12:41:53 -080026[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080027/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78142cb64b9e98bc]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080028
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030029#include "clinic/_datetimemodule.c.h"
30
Tim Peters2a799bf2002-12-16 20:18:38 +000031/* We require that C int be at least 32 bits, and use int virtually
32 * everywhere. In just a few cases we use a temp long, where a Python
33 * API returns a C long. In such cases, we have to ensure that the
34 * final result fits in a C int (this can be an issue on 64-bit boxes).
35 */
36#if SIZEOF_INT < 4
Alexander Belopolskycf86e362010-07-23 19:25:47 +000037# error "_datetime.c requires that C int have at least 32 bits"
Tim Peters2a799bf2002-12-16 20:18:38 +000038#endif
39
40#define MINYEAR 1
41#define MAXYEAR 9999
Alexander Belopolskyf03a6162010-05-27 21:42:58 +000042#define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */
Tim Peters2a799bf2002-12-16 20:18:38 +000043
44/* Nine decimal digits is easy to communicate, and leaves enough room
45 * so that two delta days can be added w/o fear of overflowing a signed
46 * 32-bit int, and with plenty of room left over to absorb any possible
47 * carries from adding seconds.
48 */
49#define MAX_DELTA_DAYS 999999999
50
51/* Rename the long macros in datetime.h to more reasonable short names. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052#define GET_YEAR PyDateTime_GET_YEAR
53#define GET_MONTH PyDateTime_GET_MONTH
54#define GET_DAY PyDateTime_GET_DAY
55#define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR
56#define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE
57#define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND
58#define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND
Tim Peters2a799bf2002-12-16 20:18:38 +000059
60/* Date accessors for date and datetime. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061#define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \
62 ((o)->data[1] = ((v) & 0x00ff)))
63#define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v))
64#define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v))
Tim Peters2a799bf2002-12-16 20:18:38 +000065
66/* Date/Time accessors for datetime. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067#define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v))
68#define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v))
69#define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v))
70#define DATE_SET_MICROSECOND(o, v) \
71 (((o)->data[7] = ((v) & 0xff0000) >> 16), \
72 ((o)->data[8] = ((v) & 0x00ff00) >> 8), \
73 ((o)->data[9] = ((v) & 0x0000ff)))
Tim Peters2a799bf2002-12-16 20:18:38 +000074
75/* Time accessors for time. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076#define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR
77#define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE
78#define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND
79#define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND
80#define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v))
81#define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v))
82#define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v))
83#define TIME_SET_MICROSECOND(o, v) \
84 (((o)->data[3] = ((v) & 0xff0000) >> 16), \
85 ((o)->data[4] = ((v) & 0x00ff00) >> 8), \
86 ((o)->data[5] = ((v) & 0x0000ff)))
Tim Peters2a799bf2002-12-16 20:18:38 +000087
88/* Delta accessors for timedelta. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089#define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days)
90#define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds)
91#define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds)
Tim Peters2a799bf2002-12-16 20:18:38 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093#define SET_TD_DAYS(o, v) ((o)->days = (v))
94#define SET_TD_SECONDS(o, v) ((o)->seconds = (v))
Tim Peters2a799bf2002-12-16 20:18:38 +000095#define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v))
96
Tim Petersa032d2e2003-01-11 00:15:54 +000097/* p is a pointer to a time or a datetime object; HASTZINFO(p) returns
98 * p->hastzinfo.
99 */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000100#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
101#define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \
102 ((PyDateTime_Time *)(p))->tzinfo : Py_None)
103#define GET_DT_TZINFO(p) (HASTZINFO(p) ? \
104 ((PyDateTime_DateTime *)(p))->tzinfo : Py_None)
Tim Peters3f606292004-03-21 23:38:41 +0000105/* M is a char or int claiming to be a valid month. The macro is equivalent
106 * to the two-sided Python test
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 * 1 <= M <= 12
Tim Peters3f606292004-03-21 23:38:41 +0000108 */
109#define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12)
110
Tim Peters2a799bf2002-12-16 20:18:38 +0000111/* Forward declarations. */
112static PyTypeObject PyDateTime_DateType;
113static PyTypeObject PyDateTime_DateTimeType;
Tim Peters2a799bf2002-12-16 20:18:38 +0000114static PyTypeObject PyDateTime_DeltaType;
115static PyTypeObject PyDateTime_TimeType;
116static PyTypeObject PyDateTime_TZInfoType;
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000117static PyTypeObject PyDateTime_TimeZoneType;
Tim Peters2a799bf2002-12-16 20:18:38 +0000118
Martin v. Löwise75fc142013-11-07 18:46:53 +0100119_Py_IDENTIFIER(as_integer_ratio);
120_Py_IDENTIFIER(fromutc);
121_Py_IDENTIFIER(isoformat);
122_Py_IDENTIFIER(strftime);
123
Tim Peters2a799bf2002-12-16 20:18:38 +0000124/* ---------------------------------------------------------------------------
125 * Math utilities.
126 */
127
128/* k = i+j overflows iff k differs in sign from both inputs,
129 * iff k^i has sign bit set and k^j has sign bit set,
130 * iff (k^i)&(k^j) has sign bit set.
131 */
132#define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0)
Tim Peters2a799bf2002-12-16 20:18:38 +0000134
135/* Compute Python divmod(x, y), returning the quotient and storing the
136 * remainder into *r. The quotient is the floor of x/y, and that's
137 * the real point of this. C will probably truncate instead (C99
138 * requires truncation; C89 left it implementation-defined).
139 * Simplification: we *require* that y > 0 here. That's appropriate
140 * for all the uses made of it. This simplifies the code and makes
141 * the overflow case impossible (divmod(LONG_MIN, -1) is the only
142 * overflow case).
143 */
144static int
145divmod(int x, int y, int *r)
146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 int quo;
Tim Peters2a799bf2002-12-16 20:18:38 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 assert(y > 0);
150 quo = x / y;
151 *r = x - quo * y;
152 if (*r < 0) {
153 --quo;
154 *r += y;
155 }
156 assert(0 <= *r && *r < y);
157 return quo;
Tim Peters2a799bf2002-12-16 20:18:38 +0000158}
159
Alexander Belopolsky1790bc42010-05-31 17:33:47 +0000160/* Nearest integer to m / n for integers m and n. Half-integer results
161 * are rounded to even.
162 */
163static PyObject *
164divide_nearest(PyObject *m, PyObject *n)
165{
166 PyObject *result;
167 PyObject *temp;
168
Mark Dickinsonfa68a612010-06-07 18:47:09 +0000169 temp = _PyLong_DivmodNear(m, n);
Alexander Belopolsky1790bc42010-05-31 17:33:47 +0000170 if (temp == NULL)
171 return NULL;
172 result = PyTuple_GET_ITEM(temp, 0);
173 Py_INCREF(result);
174 Py_DECREF(temp);
175
176 return result;
177}
178
Tim Peters2a799bf2002-12-16 20:18:38 +0000179/* ---------------------------------------------------------------------------
180 * General calendrical helper functions
181 */
182
183/* For each month ordinal in 1..12, the number of days in that month,
184 * and the number of days before that month in the same year. These
185 * are correct for non-leap years only.
186 */
187static int _days_in_month[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 0, /* unused; this vector uses 1-based indexing */
189 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
Tim Peters2a799bf2002-12-16 20:18:38 +0000190};
191
192static int _days_before_month[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 0, /* unused; this vector uses 1-based indexing */
194 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
Tim Peters2a799bf2002-12-16 20:18:38 +0000195};
196
197/* year -> 1 if leap year, else 0. */
198static int
199is_leap(int year)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 /* Cast year to unsigned. The result is the same either way, but
202 * C can generate faster code for unsigned mod than for signed
203 * mod (especially for % 4 -- a good compiler should just grab
204 * the last 2 bits when the LHS is unsigned).
205 */
206 const unsigned int ayear = (unsigned int)year;
207 return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0);
Tim Peters2a799bf2002-12-16 20:18:38 +0000208}
209
210/* year, month -> number of days in that month in that year */
211static int
212days_in_month(int year, int month)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 assert(month >= 1);
215 assert(month <= 12);
216 if (month == 2 && is_leap(year))
217 return 29;
218 else
219 return _days_in_month[month];
Tim Peters2a799bf2002-12-16 20:18:38 +0000220}
221
222/* year, month -> number of days in year preceeding first day of month */
223static int
224days_before_month(int year, int month)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 int days;
Tim Peters2a799bf2002-12-16 20:18:38 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 assert(month >= 1);
229 assert(month <= 12);
230 days = _days_before_month[month];
231 if (month > 2 && is_leap(year))
232 ++days;
233 return days;
Tim Peters2a799bf2002-12-16 20:18:38 +0000234}
235
236/* year -> number of days before January 1st of year. Remember that we
237 * start with year 1, so days_before_year(1) == 0.
238 */
239static int
240days_before_year(int year)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 int y = year - 1;
243 /* This is incorrect if year <= 0; we really want the floor
244 * here. But so long as MINYEAR is 1, the smallest year this
Alexander Belopolsky59a289d2010-10-13 22:54:34 +0000245 * can see is 1.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 */
Alexander Belopolsky59a289d2010-10-13 22:54:34 +0000247 assert (year >= 1);
248 return y*365 + y/4 - y/100 + y/400;
Tim Peters2a799bf2002-12-16 20:18:38 +0000249}
250
251/* Number of days in 4, 100, and 400 year cycles. That these have
252 * the correct values is asserted in the module init function.
253 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254#define DI4Y 1461 /* days_before_year(5); days in 4 years */
255#define DI100Y 36524 /* days_before_year(101); days in 100 years */
256#define DI400Y 146097 /* days_before_year(401); days in 400 years */
Tim Peters2a799bf2002-12-16 20:18:38 +0000257
258/* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
259static void
260ord_to_ymd(int ordinal, int *year, int *month, int *day)
261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 int n, n1, n4, n100, n400, leapyear, preceding;
Tim Peters2a799bf2002-12-16 20:18:38 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of
265 * leap years repeats exactly every 400 years. The basic strategy is
266 * to find the closest 400-year boundary at or before ordinal, then
267 * work with the offset from that boundary to ordinal. Life is much
268 * clearer if we subtract 1 from ordinal first -- then the values
269 * of ordinal at 400-year boundaries are exactly those divisible
270 * by DI400Y:
271 *
272 * D M Y n n-1
273 * -- --- ---- ---------- ----------------
274 * 31 Dec -400 -DI400Y -DI400Y -1
275 * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary
276 * ...
277 * 30 Dec 000 -1 -2
278 * 31 Dec 000 0 -1
279 * 1 Jan 001 1 0 400-year boundary
280 * 2 Jan 001 2 1
281 * 3 Jan 001 3 2
282 * ...
283 * 31 Dec 400 DI400Y DI400Y -1
284 * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary
285 */
286 assert(ordinal >= 1);
287 --ordinal;
288 n400 = ordinal / DI400Y;
289 n = ordinal % DI400Y;
290 *year = n400 * 400 + 1;
Tim Peters2a799bf2002-12-16 20:18:38 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 /* Now n is the (non-negative) offset, in days, from January 1 of
293 * year, to the desired date. Now compute how many 100-year cycles
294 * precede n.
295 * Note that it's possible for n100 to equal 4! In that case 4 full
296 * 100-year cycles precede the desired day, which implies the
297 * desired day is December 31 at the end of a 400-year cycle.
298 */
299 n100 = n / DI100Y;
300 n = n % DI100Y;
Tim Peters2a799bf2002-12-16 20:18:38 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Now compute how many 4-year cycles precede it. */
303 n4 = n / DI4Y;
304 n = n % DI4Y;
Tim Peters2a799bf2002-12-16 20:18:38 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* And now how many single years. Again n1 can be 4, and again
307 * meaning that the desired day is December 31 at the end of the
308 * 4-year cycle.
309 */
310 n1 = n / 365;
311 n = n % 365;
Tim Peters2a799bf2002-12-16 20:18:38 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 *year += n100 * 100 + n4 * 4 + n1;
314 if (n1 == 4 || n100 == 4) {
315 assert(n == 0);
316 *year -= 1;
317 *month = 12;
318 *day = 31;
319 return;
320 }
Tim Peters2a799bf2002-12-16 20:18:38 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* Now the year is correct, and n is the offset from January 1. We
323 * find the month via an estimate that's either exact or one too
324 * large.
325 */
326 leapyear = n1 == 3 && (n4 != 24 || n100 == 3);
327 assert(leapyear == is_leap(*year));
328 *month = (n + 50) >> 5;
329 preceding = (_days_before_month[*month] + (*month > 2 && leapyear));
330 if (preceding > n) {
331 /* estimate is too large */
332 *month -= 1;
333 preceding -= days_in_month(*year, *month);
334 }
335 n -= preceding;
336 assert(0 <= n);
337 assert(n < days_in_month(*year, *month));
Tim Peters2a799bf2002-12-16 20:18:38 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 *day = n + 1;
Tim Peters2a799bf2002-12-16 20:18:38 +0000340}
341
342/* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */
343static int
344ymd_to_ord(int year, int month, int day)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 return days_before_year(year) + days_before_month(year, month) + day;
Tim Peters2a799bf2002-12-16 20:18:38 +0000347}
348
349/* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */
350static int
351weekday(int year, int month, int day)
352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return (ymd_to_ord(year, month, day) + 6) % 7;
Tim Peters2a799bf2002-12-16 20:18:38 +0000354}
355
356/* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the
357 * first calendar week containing a Thursday.
358 */
359static int
360iso_week1_monday(int year)
361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */
363 /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */
364 int first_weekday = (first_day + 6) % 7;
365 /* ordinal of closest Monday at or before 1/1 */
366 int week1_monday = first_day - first_weekday;
Tim Peters2a799bf2002-12-16 20:18:38 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */
369 week1_monday += 7;
370 return week1_monday;
Tim Peters2a799bf2002-12-16 20:18:38 +0000371}
372
373/* ---------------------------------------------------------------------------
374 * Range checkers.
375 */
376
377/* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0.
378 * If not, raise OverflowError and return -1.
379 */
380static int
381check_delta_day_range(int days)
382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS)
384 return 0;
385 PyErr_Format(PyExc_OverflowError,
386 "days=%d; must have magnitude <= %d",
387 days, MAX_DELTA_DAYS);
388 return -1;
Tim Peters2a799bf2002-12-16 20:18:38 +0000389}
390
391/* Check that date arguments are in range. Return 0 if they are. If they
392 * aren't, raise ValueError and return -1.
393 */
394static int
395check_date_args(int year, int month, int day)
396{
397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (year < MINYEAR || year > MAXYEAR) {
399 PyErr_SetString(PyExc_ValueError,
400 "year is out of range");
401 return -1;
402 }
403 if (month < 1 || month > 12) {
404 PyErr_SetString(PyExc_ValueError,
405 "month must be in 1..12");
406 return -1;
407 }
408 if (day < 1 || day > days_in_month(year, month)) {
409 PyErr_SetString(PyExc_ValueError,
410 "day is out of range for month");
411 return -1;
412 }
413 return 0;
Tim Peters2a799bf2002-12-16 20:18:38 +0000414}
415
416/* Check that time arguments are in range. Return 0 if they are. If they
417 * aren't, raise ValueError and return -1.
418 */
419static int
420check_time_args(int h, int m, int s, int us)
421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (h < 0 || h > 23) {
423 PyErr_SetString(PyExc_ValueError,
424 "hour must be in 0..23");
425 return -1;
426 }
427 if (m < 0 || m > 59) {
428 PyErr_SetString(PyExc_ValueError,
429 "minute must be in 0..59");
430 return -1;
431 }
432 if (s < 0 || s > 59) {
433 PyErr_SetString(PyExc_ValueError,
434 "second must be in 0..59");
435 return -1;
436 }
437 if (us < 0 || us > 999999) {
438 PyErr_SetString(PyExc_ValueError,
439 "microsecond must be in 0..999999");
440 return -1;
441 }
442 return 0;
Tim Peters2a799bf2002-12-16 20:18:38 +0000443}
444
445/* ---------------------------------------------------------------------------
446 * Normalization utilities.
447 */
448
449/* One step of a mixed-radix conversion. A "hi" unit is equivalent to
450 * factor "lo" units. factor must be > 0. If *lo is less than 0, or
451 * at least factor, enough of *lo is converted into "hi" units so that
452 * 0 <= *lo < factor. The input values must be such that int overflow
453 * is impossible.
454 */
455static void
456normalize_pair(int *hi, int *lo, int factor)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 assert(factor > 0);
459 assert(lo != hi);
460 if (*lo < 0 || *lo >= factor) {
461 const int num_hi = divmod(*lo, factor, lo);
462 const int new_hi = *hi + num_hi;
463 assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi));
464 *hi = new_hi;
465 }
466 assert(0 <= *lo && *lo < factor);
Tim Peters2a799bf2002-12-16 20:18:38 +0000467}
468
469/* Fiddle days (d), seconds (s), and microseconds (us) so that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 * 0 <= *s < 24*3600
471 * 0 <= *us < 1000000
Tim Peters2a799bf2002-12-16 20:18:38 +0000472 * The input values must be such that the internals don't overflow.
473 * The way this routine is used, we don't get close.
474 */
475static void
476normalize_d_s_us(int *d, int *s, int *us)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (*us < 0 || *us >= 1000000) {
479 normalize_pair(s, us, 1000000);
480 /* |s| can't be bigger than about
481 * |original s| + |original us|/1000000 now.
482 */
Tim Peters2a799bf2002-12-16 20:18:38 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
485 if (*s < 0 || *s >= 24*3600) {
486 normalize_pair(d, s, 24*3600);
487 /* |d| can't be bigger than about
488 * |original d| +
489 * (|original s| + |original us|/1000000) / (24*3600) now.
490 */
491 }
492 assert(0 <= *s && *s < 24*3600);
493 assert(0 <= *us && *us < 1000000);
Tim Peters2a799bf2002-12-16 20:18:38 +0000494}
495
496/* Fiddle years (y), months (m), and days (d) so that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 * 1 <= *m <= 12
498 * 1 <= *d <= days_in_month(*y, *m)
Tim Peters2a799bf2002-12-16 20:18:38 +0000499 * The input values must be such that the internals don't overflow.
500 * The way this routine is used, we don't get close.
501 */
Alexander Belopolskyf03a6162010-05-27 21:42:58 +0000502static int
Tim Peters2a799bf2002-12-16 20:18:38 +0000503normalize_y_m_d(int *y, int *m, int *d)
504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 int dim; /* # of days in month */
Tim Peters2a799bf2002-12-16 20:18:38 +0000506
Alexander Belopolsky59a289d2010-10-13 22:54:34 +0000507 /* In actual use, m is always the month component extracted from a
508 * date/datetime object. Therefore it is always in [1, 12] range.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 */
Alexander Belopolsky59a289d2010-10-13 22:54:34 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 assert(1 <= *m && *m <= 12);
Tim Peters2a799bf2002-12-16 20:18:38 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 /* Now only day can be out of bounds (year may also be out of bounds
514 * for a datetime object, but we don't care about that here).
515 * If day is out of bounds, what to do is arguable, but at least the
516 * method here is principled and explainable.
517 */
518 dim = days_in_month(*y, *m);
519 if (*d < 1 || *d > dim) {
520 /* Move day-1 days from the first of the month. First try to
521 * get off cheap if we're only one day out of range
522 * (adjustments for timezone alone can't be worse than that).
523 */
524 if (*d == 0) {
525 --*m;
526 if (*m > 0)
527 *d = days_in_month(*y, *m);
528 else {
529 --*y;
530 *m = 12;
531 *d = 31;
532 }
533 }
534 else if (*d == dim + 1) {
535 /* move forward a day */
536 ++*m;
537 *d = 1;
538 if (*m > 12) {
539 *m = 1;
540 ++*y;
541 }
542 }
543 else {
544 int ordinal = ymd_to_ord(*y, *m, 1) +
545 *d - 1;
Alexander Belopolskyf03a6162010-05-27 21:42:58 +0000546 if (ordinal < 1 || ordinal > MAXORDINAL) {
547 goto error;
548 } else {
549 ord_to_ymd(ordinal, y, m, d);
550 return 0;
551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 }
553 }
554 assert(*m > 0);
555 assert(*d > 0);
Alexander Belopolskyf03a6162010-05-27 21:42:58 +0000556 if (MINYEAR <= *y && *y <= MAXYEAR)
557 return 0;
558 error:
559 PyErr_SetString(PyExc_OverflowError,
560 "date value out of range");
561 return -1;
562
Tim Peters2a799bf2002-12-16 20:18:38 +0000563}
564
565/* Fiddle out-of-bounds months and days so that the result makes some kind
566 * of sense. The parameters are both inputs and outputs. Returns < 0 on
567 * failure, where failure means the adjusted year is out of bounds.
568 */
569static int
570normalize_date(int *year, int *month, int *day)
571{
Alexander Belopolskyf03a6162010-05-27 21:42:58 +0000572 return normalize_y_m_d(year, month, day);
Tim Peters2a799bf2002-12-16 20:18:38 +0000573}
574
575/* Force all the datetime fields into range. The parameters are both
576 * inputs and outputs. Returns < 0 on error.
577 */
578static int
579normalize_datetime(int *year, int *month, int *day,
580 int *hour, int *minute, int *second,
581 int *microsecond)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 normalize_pair(second, microsecond, 1000000);
584 normalize_pair(minute, second, 60);
585 normalize_pair(hour, minute, 60);
586 normalize_pair(day, hour, 24);
587 return normalize_date(year, month, day);
Tim Peters2a799bf2002-12-16 20:18:38 +0000588}
589
590/* ---------------------------------------------------------------------------
Tim Petersb0c854d2003-05-17 15:57:00 +0000591 * Basic object allocation: tp_alloc implementations. These allocate
592 * Python objects of the right size and type, and do the Python object-
593 * initialization bit. If there's not enough memory, they return NULL after
594 * setting MemoryError. All data members remain uninitialized trash.
595 *
596 * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo
Tim Peters03eaf8b2003-05-18 02:24:46 +0000597 * member is needed. This is ugly, imprecise, and possibly insecure.
598 * tp_basicsize for the time and datetime types is set to the size of the
599 * struct that has room for the tzinfo member, so subclasses in Python will
600 * allocate enough space for a tzinfo member whether or not one is actually
601 * needed. That's the "ugly and imprecise" parts. The "possibly insecure"
602 * part is that PyType_GenericAlloc() (which subclasses in Python end up
603 * using) just happens today to effectively ignore the nitems argument
604 * when tp_itemsize is 0, which it is for these type objects. If that
605 * changes, perhaps the callers of tp_alloc slots in this file should
606 * be changed to force a 0 nitems argument unless the type being allocated
607 * is a base type implemented in this file (so that tp_alloc is time_alloc
608 * or datetime_alloc below, which know about the nitems abuse).
Tim Petersb0c854d2003-05-17 15:57:00 +0000609 */
610
611static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000612time_alloc(PyTypeObject *type, Py_ssize_t aware)
Tim Petersb0c854d2003-05-17 15:57:00 +0000613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyObject *self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 self = (PyObject *)
617 PyObject_MALLOC(aware ?
618 sizeof(PyDateTime_Time) :
619 sizeof(_PyDateTime_BaseTime));
620 if (self == NULL)
621 return (PyObject *)PyErr_NoMemory();
Christian Heimesecb4e6a2013-12-04 09:34:29 +0100622 (void)PyObject_INIT(self, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000624}
625
626static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000627datetime_alloc(PyTypeObject *type, Py_ssize_t aware)
Tim Petersb0c854d2003-05-17 15:57:00 +0000628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PyObject *self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 self = (PyObject *)
632 PyObject_MALLOC(aware ?
633 sizeof(PyDateTime_DateTime) :
634 sizeof(_PyDateTime_BaseDateTime));
635 if (self == NULL)
636 return (PyObject *)PyErr_NoMemory();
Christian Heimesecb4e6a2013-12-04 09:34:29 +0100637 (void)PyObject_INIT(self, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 return self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000639}
640
641/* ---------------------------------------------------------------------------
642 * Helpers for setting object fields. These work on pointers to the
643 * appropriate base class.
644 */
645
646/* For date and datetime. */
647static void
648set_date_fields(PyDateTime_Date *self, int y, int m, int d)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 self->hashcode = -1;
651 SET_YEAR(self, y);
652 SET_MONTH(self, m);
653 SET_DAY(self, d);
Tim Petersb0c854d2003-05-17 15:57:00 +0000654}
655
656/* ---------------------------------------------------------------------------
657 * Create various objects, mostly without range checking.
658 */
659
660/* Create a date instance with no range checking. */
661static PyObject *
662new_date_ex(int year, int month, int day, PyTypeObject *type)
663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 PyDateTime_Date *self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 self = (PyDateTime_Date *) (type->tp_alloc(type, 0));
667 if (self != NULL)
668 set_date_fields(self, year, month, day);
669 return (PyObject *) self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000670}
671
672#define new_date(year, month, day) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 new_date_ex(year, month, day, &PyDateTime_DateType)
Tim Petersb0c854d2003-05-17 15:57:00 +0000674
675/* Create a datetime instance with no range checking. */
676static PyObject *
677new_datetime_ex(int year, int month, int day, int hour, int minute,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 int second, int usecond, PyObject *tzinfo, PyTypeObject *type)
Tim Petersb0c854d2003-05-17 15:57:00 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 PyDateTime_DateTime *self;
681 char aware = tzinfo != Py_None;
Tim Petersb0c854d2003-05-17 15:57:00 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware));
684 if (self != NULL) {
685 self->hastzinfo = aware;
686 set_date_fields((PyDateTime_Date *)self, year, month, day);
687 DATE_SET_HOUR(self, hour);
688 DATE_SET_MINUTE(self, minute);
689 DATE_SET_SECOND(self, second);
690 DATE_SET_MICROSECOND(self, usecond);
691 if (aware) {
692 Py_INCREF(tzinfo);
693 self->tzinfo = tzinfo;
694 }
695 }
696 return (PyObject *)self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000697}
698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699#define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \
700 new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \
701 &PyDateTime_DateTimeType)
Tim Petersb0c854d2003-05-17 15:57:00 +0000702
703/* Create a time instance with no range checking. */
704static PyObject *
705new_time_ex(int hour, int minute, int second, int usecond,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject *tzinfo, PyTypeObject *type)
Tim Petersb0c854d2003-05-17 15:57:00 +0000707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyDateTime_Time *self;
709 char aware = tzinfo != Py_None;
Tim Petersb0c854d2003-05-17 15:57:00 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 self = (PyDateTime_Time *) (type->tp_alloc(type, aware));
712 if (self != NULL) {
713 self->hastzinfo = aware;
714 self->hashcode = -1;
715 TIME_SET_HOUR(self, hour);
716 TIME_SET_MINUTE(self, minute);
717 TIME_SET_SECOND(self, second);
718 TIME_SET_MICROSECOND(self, usecond);
719 if (aware) {
720 Py_INCREF(tzinfo);
721 self->tzinfo = tzinfo;
722 }
723 }
724 return (PyObject *)self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000725}
726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727#define new_time(hh, mm, ss, us, tzinfo) \
728 new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType)
Tim Petersb0c854d2003-05-17 15:57:00 +0000729
730/* Create a timedelta instance. Normalize the members iff normalize is
731 * true. Passing false is a speed optimization, if you know for sure
732 * that seconds and microseconds are already in their proper ranges. In any
733 * case, raises OverflowError and returns NULL if the normalized days is out
734 * of range).
735 */
736static PyObject *
737new_delta_ex(int days, int seconds, int microseconds, int normalize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 PyTypeObject *type)
Tim Petersb0c854d2003-05-17 15:57:00 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyDateTime_Delta *self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 if (normalize)
743 normalize_d_s_us(&days, &seconds, &microseconds);
744 assert(0 <= seconds && seconds < 24*3600);
745 assert(0 <= microseconds && microseconds < 1000000);
Tim Petersb0c854d2003-05-17 15:57:00 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (check_delta_day_range(days) < 0)
748 return NULL;
Tim Petersb0c854d2003-05-17 15:57:00 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 self = (PyDateTime_Delta *) (type->tp_alloc(type, 0));
751 if (self != NULL) {
752 self->hashcode = -1;
753 SET_TD_DAYS(self, days);
754 SET_TD_SECONDS(self, seconds);
755 SET_TD_MICROSECONDS(self, microseconds);
756 }
757 return (PyObject *) self;
Tim Petersb0c854d2003-05-17 15:57:00 +0000758}
759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760#define new_delta(d, s, us, normalize) \
761 new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType)
Tim Petersb0c854d2003-05-17 15:57:00 +0000762
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000763
764typedef struct
765{
766 PyObject_HEAD
767 PyObject *offset;
768 PyObject *name;
769} PyDateTime_TimeZone;
770
Victor Stinner6ced7c42011-03-21 18:15:42 +0100771/* The interned UTC timezone instance */
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +0000772static PyObject *PyDateTime_TimeZone_UTC;
Alexander Belopolskya4415142012-06-08 12:33:09 -0400773/* The interned Epoch datetime instance */
774static PyObject *PyDateTime_Epoch;
Alexander Belopolskya11d8c02010-07-06 23:19:45 +0000775
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000776/* Create new timezone instance checking offset range. This
777 function does not check the name argument. Caller must assure
778 that offset is a timedelta instance and name is either NULL
779 or a unicode object. */
780static PyObject *
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +0000781create_timezone(PyObject *offset, PyObject *name)
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000782{
783 PyDateTime_TimeZone *self;
784 PyTypeObject *type = &PyDateTime_TimeZoneType;
785
786 assert(offset != NULL);
787 assert(PyDelta_Check(offset));
788 assert(name == NULL || PyUnicode_Check(name));
789
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +0000790 self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0));
791 if (self == NULL) {
792 return NULL;
793 }
794 Py_INCREF(offset);
795 self->offset = offset;
796 Py_XINCREF(name);
797 self->name = name;
798 return (PyObject *)self;
799}
800
801static int delta_bool(PyDateTime_Delta *self);
802
803static PyObject *
804new_timezone(PyObject *offset, PyObject *name)
805{
806 assert(offset != NULL);
807 assert(PyDelta_Check(offset));
808 assert(name == NULL || PyUnicode_Check(name));
809
810 if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) {
811 Py_INCREF(PyDateTime_TimeZone_UTC);
812 return PyDateTime_TimeZone_UTC;
813 }
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000814 if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) {
815 PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
Alexander Belopolsky31227ca2012-06-22 13:23:21 -0400816 " representing a whole number of minutes,"
817 " not %R.", offset);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000818 return NULL;
819 }
820 if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) ||
821 GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) {
822 PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
823 " strictly between -timedelta(hours=24) and"
Alexander Belopolsky31227ca2012-06-22 13:23:21 -0400824 " timedelta(hours=24),"
825 " not %R.", offset);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000826 return NULL;
827 }
828
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +0000829 return create_timezone(offset, name);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +0000830}
831
Tim Petersb0c854d2003-05-17 15:57:00 +0000832/* ---------------------------------------------------------------------------
Tim Peters2a799bf2002-12-16 20:18:38 +0000833 * tzinfo helpers.
834 */
835
Tim Peters855fe882002-12-22 03:43:39 +0000836/* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not
837 * raise TypeError and return -1.
838 */
839static int
840check_tzinfo_subclass(PyObject *p)
841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 if (p == Py_None || PyTZInfo_Check(p))
843 return 0;
844 PyErr_Format(PyExc_TypeError,
845 "tzinfo argument must be None or of a tzinfo subclass, "
846 "not type '%s'",
847 Py_TYPE(p)->tp_name);
848 return -1;
Tim Peters855fe882002-12-22 03:43:39 +0000849}
850
Tim Peters2a799bf2002-12-16 20:18:38 +0000851/* If self has a tzinfo member, return a BORROWED reference to it. Else
852 * return NULL, which is NOT AN ERROR. There are no error returns here,
853 * and the caller must not decref the result.
854 */
855static PyObject *
856get_tzinfo_member(PyObject *self)
857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 PyObject *tzinfo = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (PyDateTime_Check(self) && HASTZINFO(self))
861 tzinfo = ((PyDateTime_DateTime *)self)->tzinfo;
862 else if (PyTime_Check(self) && HASTZINFO(self))
863 tzinfo = ((PyDateTime_Time *)self)->tzinfo;
Tim Peters2a799bf2002-12-16 20:18:38 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return tzinfo;
Tim Peters2a799bf2002-12-16 20:18:38 +0000866}
867
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000868/* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must
869 * be an instance of the tzinfo class. If the method returns None, this
870 * returns None. If the method doesn't return None or timedelta, TypeError is
871 * raised and this returns NULL. If it returns a timedelta and the value is
872 * out of range or isn't a whole number of minutes, ValueError is raised and
873 * this returns NULL. Else result is returned.
Tim Peters2a799bf2002-12-16 20:18:38 +0000874 */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000875static PyObject *
876call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg)
Tim Peters2a799bf2002-12-16 20:18:38 +0000877{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000878 PyObject *offset;
Tim Peters2a799bf2002-12-16 20:18:38 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 assert(tzinfo != NULL);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000881 assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 assert(tzinfoarg != NULL);
Tim Peters2a799bf2002-12-16 20:18:38 +0000883
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000884 if (tzinfo == Py_None)
885 Py_RETURN_NONE;
886 offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg);
887 if (offset == Py_None || offset == NULL)
888 return offset;
889 if (PyDelta_Check(offset)) {
890 if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) {
891 Py_DECREF(offset);
892 PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
893 " representing a whole number of minutes");
894 return NULL;
895 }
896 if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) ||
897 GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) {
898 Py_DECREF(offset);
899 PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
900 " strictly between -timedelta(hours=24) and"
901 " timedelta(hours=24).");
902 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 }
904 }
905 else {
906 PyErr_Format(PyExc_TypeError,
907 "tzinfo.%s() must return None or "
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000908 "timedelta, not '%.200s'",
909 name, Py_TYPE(offset)->tp_name);
Raymond Hettinger5a2146a2014-07-25 14:59:48 -0700910 Py_DECREF(offset);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000911 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 }
Tim Peters2a799bf2002-12-16 20:18:38 +0000913
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000914 return offset;
Tim Peters2a799bf2002-12-16 20:18:38 +0000915}
916
917/* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the
918 * result. tzinfo must be an instance of the tzinfo class. If utcoffset()
919 * returns None, call_utcoffset returns 0 and sets *none to 1. If uctoffset()
Tim Peters397301e2003-01-02 21:28:08 +0000920 * doesn't return None or timedelta, TypeError is raised and this returns -1.
921 * If utcoffset() returns an invalid timedelta (out of range, or not a whole
922 * # of minutes), ValueError is raised and this returns -1. Else *none is
923 * set to 0 and the offset is returned (as int # of minutes east of UTC).
Tim Peters2a799bf2002-12-16 20:18:38 +0000924 */
Tim Peters855fe882002-12-22 03:43:39 +0000925static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000926call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg)
927{
928 return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg);
Tim Peters855fe882002-12-22 03:43:39 +0000929}
930
Tim Peters2a799bf2002-12-16 20:18:38 +0000931/* Call tzinfo.dst(tzinfoarg), and extract an integer from the
932 * result. tzinfo must be an instance of the tzinfo class. If dst()
933 * returns None, call_dst returns 0 and sets *none to 1. If dst()
Tim Peters397301e2003-01-02 21:28:08 +0000934 & doesn't return None or timedelta, TypeError is raised and this
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000935 * returns -1. If dst() returns an invalid timedelta for a UTC offset,
Tim Peters397301e2003-01-02 21:28:08 +0000936 * ValueError is raised and this returns -1. Else *none is set to 0 and
937 * the offset is returned (as an int # of minutes east of UTC).
Tim Peters2a799bf2002-12-16 20:18:38 +0000938 */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000939static PyObject *
940call_dst(PyObject *tzinfo, PyObject *tzinfoarg)
Tim Peters2a799bf2002-12-16 20:18:38 +0000941{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000942 return call_tzinfo_method(tzinfo, "dst", tzinfoarg);
Tim Peters2a799bf2002-12-16 20:18:38 +0000943}
944
Tim Petersbad8ff02002-12-30 20:52:32 +0000945/* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be
Tim Peters855fe882002-12-22 03:43:39 +0000946 * an instance of the tzinfo class or None. If tzinfo isn't None, and
Tim Petersbad8ff02002-12-30 20:52:32 +0000947 * tzname() doesn't return None or a string, TypeError is raised and this
Guido van Rossume3d1d412007-05-23 21:24:35 +0000948 * returns NULL. If the result is a string, we ensure it is a Unicode
949 * string.
Tim Peters2a799bf2002-12-16 20:18:38 +0000950 */
951static PyObject *
Tim Petersbad8ff02002-12-30 20:52:32 +0000952call_tzname(PyObject *tzinfo, PyObject *tzinfoarg)
Tim Peters2a799bf2002-12-16 20:18:38 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200955 _Py_IDENTIFIER(tzname);
Tim Peters2a799bf2002-12-16 20:18:38 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 assert(tzinfo != NULL);
958 assert(check_tzinfo_subclass(tzinfo) >= 0);
959 assert(tzinfoarg != NULL);
Tim Peters2a799bf2002-12-16 20:18:38 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (tzinfo == Py_None)
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000962 Py_RETURN_NONE;
Tim Peters2a799bf2002-12-16 20:18:38 +0000963
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200964 result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000965
966 if (result == NULL || result == Py_None)
967 return result;
968
969 if (!PyUnicode_Check(result)) {
970 PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must "
971 "return None or a string, not '%s'",
972 Py_TYPE(result)->tp_name);
973 Py_DECREF(result);
974 result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +0000976
977 return result;
Tim Peters00237032002-12-27 02:21:51 +0000978}
979
Tim Peters2a799bf2002-12-16 20:18:38 +0000980/* repr is like "someclass(arg1, arg2)". If tzinfo isn't None,
981 * stuff
982 * ", tzinfo=" + repr(tzinfo)
983 * before the closing ")".
984 */
985static PyObject *
986append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo)
987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 PyObject *temp;
Tim Peters2a799bf2002-12-16 20:18:38 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 assert(PyUnicode_Check(repr));
991 assert(tzinfo);
992 if (tzinfo == Py_None)
993 return repr;
994 /* Get rid of the trailing ')'. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200995 assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')');
996 temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_DECREF(repr);
998 if (temp == NULL)
999 return NULL;
1000 repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo);
1001 Py_DECREF(temp);
1002 return repr;
Tim Peters2a799bf2002-12-16 20:18:38 +00001003}
1004
1005/* ---------------------------------------------------------------------------
1006 * String format helpers.
1007 */
1008
1009static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00001010format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
Tim Peters2a799bf2002-12-16 20:18:38 +00001011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 static const char *DayNames[] = {
1013 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
1014 };
1015 static const char *MonthNames[] = {
1016 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1017 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1018 };
Tim Peters2a799bf2002-12-16 20:18:38 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date));
Tim Peters2a799bf2002-12-16 20:18:38 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d",
1023 DayNames[wday], MonthNames[GET_MONTH(date)-1],
1024 GET_DAY(date), hours, minutes, seconds,
1025 GET_YEAR(date));
Tim Peters2a799bf2002-12-16 20:18:38 +00001026}
1027
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001028static PyObject *delta_negative(PyDateTime_Delta *self);
1029
Tim Peters2a799bf2002-12-16 20:18:38 +00001030/* Add an hours & minutes UTC offset string to buf. buf has no more than
1031 * buflen bytes remaining. The UTC offset is gotten by calling
1032 * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into
1033 * *buf, and that's all. Else the returned value is checked for sanity (an
1034 * integer in range), and if that's OK it's converted to an hours & minutes
1035 * string of the form
1036 * sign HH sep MM
1037 * Returns 0 if everything is OK. If the return value from utcoffset() is
1038 * bogus, an appropriate exception is set and -1 is returned.
1039 */
1040static int
Tim Peters328fff72002-12-20 01:31:27 +00001041format_utcoffset(char *buf, size_t buflen, const char *sep,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyObject *tzinfo, PyObject *tzinfoarg)
Tim Peters2a799bf2002-12-16 20:18:38 +00001043{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001044 PyObject *offset;
1045 int hours, minutes, seconds;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 char sign;
Tim Peters2a799bf2002-12-16 20:18:38 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 assert(buflen >= 1);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001049
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001050 offset = call_utcoffset(tzinfo, tzinfoarg);
1051 if (offset == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return -1;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001053 if (offset == Py_None) {
1054 Py_DECREF(offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 *buf = '\0';
1056 return 0;
1057 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001058 /* Offset is normalized, so it is negative if days < 0 */
1059 if (GET_TD_DAYS(offset) < 0) {
1060 PyObject *temp = offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 sign = '-';
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001062 offset = delta_negative((PyDateTime_Delta *)offset);
1063 Py_DECREF(temp);
1064 if (offset == NULL)
1065 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001067 else {
1068 sign = '+';
1069 }
1070 /* Offset is not negative here. */
1071 seconds = GET_TD_SECONDS(offset);
1072 Py_DECREF(offset);
1073 minutes = divmod(seconds, 60, &seconds);
1074 hours = divmod(minutes, 60, &minutes);
1075 assert(seconds == 0);
1076 /* XXX ignore sub-minute data, curently not allowed. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 return 0;
Tim Peters2a799bf2002-12-16 20:18:38 +00001080}
1081
Guido van Rossumd8595fe2007-05-23 21:36:49 +00001082static PyObject *
1083make_Zreplacement(PyObject *object, PyObject *tzinfoarg)
1084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 PyObject *temp;
1086 PyObject *tzinfo = get_tzinfo_member(object);
1087 PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001088 _Py_IDENTIFIER(replace);
Victor Stinner9e30aa52011-11-21 02:49:52 +01001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 if (Zreplacement == NULL)
1091 return NULL;
1092 if (tzinfo == Py_None || tzinfo == NULL)
1093 return Zreplacement;
Neal Norwitzaea70e02007-08-12 04:32:26 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 assert(tzinfoarg != NULL);
1096 temp = call_tzname(tzinfo, tzinfoarg);
1097 if (temp == NULL)
1098 goto Error;
1099 if (temp == Py_None) {
1100 Py_DECREF(temp);
1101 return Zreplacement;
1102 }
Neal Norwitzaea70e02007-08-12 04:32:26 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 assert(PyUnicode_Check(temp));
1105 /* Since the tzname is getting stuffed into the
1106 * format, we have to double any % signs so that
1107 * strftime doesn't treat them as format codes.
1108 */
1109 Py_DECREF(Zreplacement);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001110 Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Py_DECREF(temp);
1112 if (Zreplacement == NULL)
1113 return NULL;
1114 if (!PyUnicode_Check(Zreplacement)) {
1115 PyErr_SetString(PyExc_TypeError,
1116 "tzname.replace() did not return a string");
1117 goto Error;
1118 }
1119 return Zreplacement;
Guido van Rossumd8595fe2007-05-23 21:36:49 +00001120
1121 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 Py_DECREF(Zreplacement);
1123 return NULL;
Guido van Rossumd8595fe2007-05-23 21:36:49 +00001124}
1125
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001126static PyObject *
1127make_freplacement(PyObject *object)
1128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 char freplacement[64];
1130 if (PyTime_Check(object))
1131 sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object));
1132 else if (PyDateTime_Check(object))
1133 sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object));
1134 else
1135 sprintf(freplacement, "%06d", 0);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 return PyBytes_FromStringAndSize(freplacement, strlen(freplacement));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001138}
1139
Tim Peters2a799bf2002-12-16 20:18:38 +00001140/* I sure don't want to reproduce the strftime code from the time module,
1141 * so this imports the module and calls it. All the hair is due to
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001142 * giving special meanings to the %z, %Z and %f format codes via a
1143 * preprocessing step on the format string.
Tim Petersbad8ff02002-12-30 20:52:32 +00001144 * tzinfoarg is the argument to pass to the object's tzinfo method, if
1145 * needed.
Tim Peters2a799bf2002-12-16 20:18:38 +00001146 */
1147static PyObject *
Tim Petersbad8ff02002-12-30 20:52:32 +00001148wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 PyObject *tzinfoarg)
Tim Peters2a799bf2002-12-16 20:18:38 +00001150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 PyObject *result = NULL; /* guilty until proved innocent */
Tim Peters2a799bf2002-12-16 20:18:38 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 PyObject *zreplacement = NULL; /* py string, replacement for %z */
1154 PyObject *Zreplacement = NULL; /* py string, replacement for %Z */
1155 PyObject *freplacement = NULL; /* py string, replacement for %f */
Tim Peters2a799bf2002-12-16 20:18:38 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 const char *pin; /* pointer to next char in input format */
1158 Py_ssize_t flen; /* length of input format */
1159 char ch; /* next char in input format */
Tim Peters2a799bf2002-12-16 20:18:38 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PyObject *newfmt = NULL; /* py string, the output format */
1162 char *pnew; /* pointer to available byte in output format */
1163 size_t totalnew; /* number bytes total in output format buffer,
1164 exclusive of trailing \0 */
1165 size_t usednew; /* number bytes used so far in output format buffer */
Tim Peters2a799bf2002-12-16 20:18:38 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 const char *ptoappend; /* ptr to string to append to output buffer */
1168 Py_ssize_t ntoappend; /* # of bytes to append to output buffer */
Tim Peters2a799bf2002-12-16 20:18:38 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 assert(object && format && timetuple);
1171 assert(PyUnicode_Check(format));
1172 /* Convert the input format to a C string and size */
1173 pin = _PyUnicode_AsStringAndSize(format, &flen);
1174 if (!pin)
1175 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* Scan the input format, looking for %z/%Z/%f escapes, building
1178 * a new format. Since computing the replacements for those codes
1179 * is expensive, don't unless they're actually used.
1180 */
1181 if (flen > INT_MAX - 1) {
1182 PyErr_NoMemory();
1183 goto Done;
1184 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 totalnew = flen + 1; /* realistic if no %z/%Z */
1187 newfmt = PyBytes_FromStringAndSize(NULL, totalnew);
1188 if (newfmt == NULL) goto Done;
1189 pnew = PyBytes_AsString(newfmt);
1190 usednew = 0;
Tim Peters2a799bf2002-12-16 20:18:38 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 while ((ch = *pin++) != '\0') {
1193 if (ch != '%') {
1194 ptoappend = pin - 1;
1195 ntoappend = 1;
1196 }
1197 else if ((ch = *pin++) == '\0') {
1198 /* There's a lone trailing %; doesn't make sense. */
1199 PyErr_SetString(PyExc_ValueError, "strftime format "
1200 "ends with raw %");
1201 goto Done;
1202 }
1203 /* A % has been seen and ch is the character after it. */
1204 else if (ch == 'z') {
1205 if (zreplacement == NULL) {
1206 /* format utcoffset */
1207 char buf[100];
1208 PyObject *tzinfo = get_tzinfo_member(object);
1209 zreplacement = PyBytes_FromStringAndSize("", 0);
1210 if (zreplacement == NULL) goto Done;
1211 if (tzinfo != Py_None && tzinfo != NULL) {
1212 assert(tzinfoarg != NULL);
1213 if (format_utcoffset(buf,
1214 sizeof(buf),
1215 "",
1216 tzinfo,
1217 tzinfoarg) < 0)
1218 goto Done;
1219 Py_DECREF(zreplacement);
1220 zreplacement =
1221 PyBytes_FromStringAndSize(buf,
1222 strlen(buf));
1223 if (zreplacement == NULL)
1224 goto Done;
1225 }
1226 }
1227 assert(zreplacement != NULL);
1228 ptoappend = PyBytes_AS_STRING(zreplacement);
1229 ntoappend = PyBytes_GET_SIZE(zreplacement);
1230 }
1231 else if (ch == 'Z') {
1232 /* format tzname */
1233 if (Zreplacement == NULL) {
1234 Zreplacement = make_Zreplacement(object,
1235 tzinfoarg);
1236 if (Zreplacement == NULL)
1237 goto Done;
1238 }
1239 assert(Zreplacement != NULL);
1240 assert(PyUnicode_Check(Zreplacement));
1241 ptoappend = _PyUnicode_AsStringAndSize(Zreplacement,
1242 &ntoappend);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001243 if (ptoappend == NULL)
1244 goto Done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 }
1246 else if (ch == 'f') {
1247 /* format microseconds */
1248 if (freplacement == NULL) {
1249 freplacement = make_freplacement(object);
1250 if (freplacement == NULL)
1251 goto Done;
1252 }
1253 assert(freplacement != NULL);
1254 assert(PyBytes_Check(freplacement));
1255 ptoappend = PyBytes_AS_STRING(freplacement);
1256 ntoappend = PyBytes_GET_SIZE(freplacement);
1257 }
1258 else {
1259 /* percent followed by neither z nor Z */
1260 ptoappend = pin - 2;
1261 ntoappend = 2;
1262 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Append the ntoappend chars starting at ptoappend to
1265 * the new format.
1266 */
1267 if (ntoappend == 0)
1268 continue;
1269 assert(ptoappend != NULL);
1270 assert(ntoappend > 0);
1271 while (usednew + ntoappend > totalnew) {
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001272 if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 PyErr_NoMemory();
1274 goto Done;
1275 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001276 totalnew <<= 1;
1277 if (_PyBytes_Resize(&newfmt, totalnew) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 goto Done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 pnew = PyBytes_AsString(newfmt) + usednew;
1280 }
1281 memcpy(pnew, ptoappend, ntoappend);
1282 pnew += ntoappend;
1283 usednew += ntoappend;
1284 assert(usednew <= totalnew);
1285 } /* end while() */
Tim Peters2a799bf2002-12-16 20:18:38 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (_PyBytes_Resize(&newfmt, usednew) < 0)
1288 goto Done;
1289 {
1290 PyObject *format;
1291 PyObject *time = PyImport_ImportModuleNoBlock("time");
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (time == NULL)
1294 goto Done;
1295 format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt));
1296 if (format != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001297 result = _PyObject_CallMethodId(time, &PyId_strftime, "OO",
1298 format, timetuple, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 Py_DECREF(format);
1300 }
1301 Py_DECREF(time);
1302 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001303 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 Py_XDECREF(freplacement);
1305 Py_XDECREF(zreplacement);
1306 Py_XDECREF(Zreplacement);
1307 Py_XDECREF(newfmt);
1308 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001309}
1310
Tim Peters2a799bf2002-12-16 20:18:38 +00001311/* ---------------------------------------------------------------------------
1312 * Wrap functions from the time module. These aren't directly available
1313 * from C. Perhaps they should be.
1314 */
1315
1316/* Call time.time() and return its result (a Python float). */
1317static PyObject *
Guido van Rossumbd43e912002-12-16 20:34:55 +00001318time_time(void)
Tim Peters2a799bf2002-12-16 20:18:38 +00001319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyObject *result = NULL;
1321 PyObject *time = PyImport_ImportModuleNoBlock("time");
Tim Peters2a799bf2002-12-16 20:18:38 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (time != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001324 _Py_IDENTIFIER(time);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001325
1326 result = _PyObject_CallMethodId(time, &PyId_time, "()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 Py_DECREF(time);
1328 }
1329 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001330}
1331
1332/* Build a time.struct_time. The weekday and day number are automatically
1333 * computed from the y,m,d args.
1334 */
1335static PyObject *
1336build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
1337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 PyObject *time;
1339 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 time = PyImport_ImportModuleNoBlock("time");
1342 if (time != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001343 _Py_IDENTIFIER(struct_time);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001344
1345 result = _PyObject_CallMethodId(time, &PyId_struct_time,
1346 "((iiiiiiiii))",
1347 y, m, d,
1348 hh, mm, ss,
1349 weekday(y, m, d),
1350 days_before_month(y, m) + d,
1351 dstflag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_DECREF(time);
1353 }
1354 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001355}
1356
1357/* ---------------------------------------------------------------------------
1358 * Miscellaneous helpers.
1359 */
1360
Mark Dickinsone94c6792009-02-02 20:36:42 +00001361/* For various reasons, we need to use tp_richcompare instead of tp_reserved.
Tim Peters2a799bf2002-12-16 20:18:38 +00001362 * The comparisons here all most naturally compute a cmp()-like result.
1363 * This little helper turns that into a bool result for rich comparisons.
1364 */
1365static PyObject *
1366diff_to_bool(int diff, int op)
1367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyObject *result;
1369 int istrue;
Tim Peters2a799bf2002-12-16 20:18:38 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 switch (op) {
1372 case Py_EQ: istrue = diff == 0; break;
1373 case Py_NE: istrue = diff != 0; break;
1374 case Py_LE: istrue = diff <= 0; break;
1375 case Py_GE: istrue = diff >= 0; break;
1376 case Py_LT: istrue = diff < 0; break;
1377 case Py_GT: istrue = diff > 0; break;
1378 default:
1379 assert(! "op unknown");
1380 istrue = 0; /* To shut up compiler */
1381 }
1382 result = istrue ? Py_True : Py_False;
1383 Py_INCREF(result);
1384 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001385}
1386
Tim Peters07534a62003-02-07 22:50:28 +00001387/* Raises a "can't compare" TypeError and returns NULL. */
1388static PyObject *
1389cmperror(PyObject *a, PyObject *b)
1390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PyErr_Format(PyExc_TypeError,
1392 "can't compare %s to %s",
1393 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
1394 return NULL;
Tim Peters07534a62003-02-07 22:50:28 +00001395}
1396
Tim Peters2a799bf2002-12-16 20:18:38 +00001397/* ---------------------------------------------------------------------------
Tim Peters2a799bf2002-12-16 20:18:38 +00001398 * Cached Python objects; these are set by the module init function.
1399 */
1400
1401/* Conversion factors. */
Alexander Belopolsky790d2692013-08-04 14:51:35 -04001402static PyObject *one = NULL; /* 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403static PyObject *us_per_ms = NULL; /* 1000 */
1404static PyObject *us_per_second = NULL; /* 1000000 */
1405static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */
Serhiy Storchaka95949422013-08-27 19:40:23 +03001406static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */
1407static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */
1408static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python int */
Tim Peters2a799bf2002-12-16 20:18:38 +00001409static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */
1410
Tim Peters2a799bf2002-12-16 20:18:38 +00001411/* ---------------------------------------------------------------------------
1412 * Class implementations.
1413 */
1414
1415/*
1416 * PyDateTime_Delta implementation.
1417 */
1418
1419/* Convert a timedelta to a number of us,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 * (24*3600*self.days + self.seconds)*1000000 + self.microseconds
Serhiy Storchaka95949422013-08-27 19:40:23 +03001421 * as a Python int.
Tim Peters2a799bf2002-12-16 20:18:38 +00001422 * Doing mixed-radix arithmetic by hand instead is excruciating in C,
1423 * due to ubiquitous overflow possibilities.
1424 */
1425static PyObject *
1426delta_to_microseconds(PyDateTime_Delta *self)
1427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 PyObject *x1 = NULL;
1429 PyObject *x2 = NULL;
1430 PyObject *x3 = NULL;
1431 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 x1 = PyLong_FromLong(GET_TD_DAYS(self));
1434 if (x1 == NULL)
1435 goto Done;
1436 x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */
1437 if (x2 == NULL)
1438 goto Done;
1439 Py_DECREF(x1);
1440 x1 = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 /* x2 has days in seconds */
1443 x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */
1444 if (x1 == NULL)
1445 goto Done;
1446 x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */
1447 if (x3 == NULL)
1448 goto Done;
1449 Py_DECREF(x1);
1450 Py_DECREF(x2);
Brett Cannonb94767f2011-02-22 20:15:44 +00001451 /* x1 = */ x2 = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 /* x3 has days+seconds in seconds */
1454 x1 = PyNumber_Multiply(x3, us_per_second); /* us */
1455 if (x1 == NULL)
1456 goto Done;
1457 Py_DECREF(x3);
1458 x3 = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* x1 has days+seconds in us */
1461 x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self));
1462 if (x2 == NULL)
1463 goto Done;
1464 result = PyNumber_Add(x1, x2);
Tim Peters2a799bf2002-12-16 20:18:38 +00001465
1466Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 Py_XDECREF(x1);
1468 Py_XDECREF(x2);
1469 Py_XDECREF(x3);
1470 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001471}
1472
Serhiy Storchaka95949422013-08-27 19:40:23 +03001473/* Convert a number of us (as a Python int) to a timedelta.
Tim Peters2a799bf2002-12-16 20:18:38 +00001474 */
1475static PyObject *
Tim Petersb0c854d2003-05-17 15:57:00 +00001476microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
Tim Peters2a799bf2002-12-16 20:18:38 +00001477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 int us;
1479 int s;
1480 int d;
1481 long temp;
Tim Peters2a799bf2002-12-16 20:18:38 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 PyObject *tuple = NULL;
1484 PyObject *num = NULL;
1485 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 tuple = PyNumber_Divmod(pyus, us_per_second);
1488 if (tuple == NULL)
1489 goto Done;
Tim Peters2a799bf2002-12-16 20:18:38 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 num = PyTuple_GetItem(tuple, 1); /* us */
1492 if (num == NULL)
1493 goto Done;
1494 temp = PyLong_AsLong(num);
1495 num = NULL;
1496 if (temp == -1 && PyErr_Occurred())
1497 goto Done;
1498 assert(0 <= temp && temp < 1000000);
1499 us = (int)temp;
1500 if (us < 0) {
1501 /* The divisor was positive, so this must be an error. */
1502 assert(PyErr_Occurred());
1503 goto Done;
1504 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 num = PyTuple_GetItem(tuple, 0); /* leftover seconds */
1507 if (num == NULL)
1508 goto Done;
1509 Py_INCREF(num);
1510 Py_DECREF(tuple);
Tim Peters2a799bf2002-12-16 20:18:38 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 tuple = PyNumber_Divmod(num, seconds_per_day);
1513 if (tuple == NULL)
1514 goto Done;
1515 Py_DECREF(num);
Tim Peters2a799bf2002-12-16 20:18:38 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 num = PyTuple_GetItem(tuple, 1); /* seconds */
1518 if (num == NULL)
1519 goto Done;
1520 temp = PyLong_AsLong(num);
1521 num = NULL;
1522 if (temp == -1 && PyErr_Occurred())
1523 goto Done;
1524 assert(0 <= temp && temp < 24*3600);
1525 s = (int)temp;
Tim Peters0b0f41c2002-12-19 01:44:38 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (s < 0) {
1528 /* The divisor was positive, so this must be an error. */
1529 assert(PyErr_Occurred());
1530 goto Done;
1531 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 num = PyTuple_GetItem(tuple, 0); /* leftover days */
1534 if (num == NULL)
1535 goto Done;
1536 Py_INCREF(num);
1537 temp = PyLong_AsLong(num);
1538 if (temp == -1 && PyErr_Occurred())
1539 goto Done;
1540 d = (int)temp;
1541 if ((long)d != temp) {
1542 PyErr_SetString(PyExc_OverflowError, "normalized days too "
1543 "large to fit in a C int");
1544 goto Done;
1545 }
1546 result = new_delta_ex(d, s, us, 0, type);
Tim Peters2a799bf2002-12-16 20:18:38 +00001547
1548Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 Py_XDECREF(tuple);
1550 Py_XDECREF(num);
1551 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001552}
1553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554#define microseconds_to_delta(pymicros) \
1555 microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType)
Tim Petersb0c854d2003-05-17 15:57:00 +00001556
Tim Peters2a799bf2002-12-16 20:18:38 +00001557static PyObject *
1558multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
1559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PyObject *pyus_in;
1561 PyObject *pyus_out;
1562 PyObject *result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 pyus_in = delta_to_microseconds(delta);
1565 if (pyus_in == NULL)
1566 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 pyus_out = PyNumber_Multiply(pyus_in, intobj);
1569 Py_DECREF(pyus_in);
1570 if (pyus_out == NULL)
1571 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 result = microseconds_to_delta(pyus_out);
1574 Py_DECREF(pyus_out);
1575 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001576}
1577
1578static PyObject *
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001579multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
1580{
1581 PyObject *result = NULL;
1582 PyObject *pyus_in = NULL, *temp, *pyus_out;
1583 PyObject *ratio = NULL;
1584
1585 pyus_in = delta_to_microseconds(delta);
1586 if (pyus_in == NULL)
1587 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001588 ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL);
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001589 if (ratio == NULL)
1590 goto error;
1591 temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0));
1592 Py_DECREF(pyus_in);
1593 pyus_in = NULL;
1594 if (temp == NULL)
1595 goto error;
1596 pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1));
1597 Py_DECREF(temp);
1598 if (pyus_out == NULL)
1599 goto error;
1600 result = microseconds_to_delta(pyus_out);
1601 Py_DECREF(pyus_out);
1602 error:
1603 Py_XDECREF(pyus_in);
1604 Py_XDECREF(ratio);
1605
1606 return result;
1607}
1608
1609static PyObject *
Tim Peters2a799bf2002-12-16 20:18:38 +00001610divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj)
1611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 PyObject *pyus_in;
1613 PyObject *pyus_out;
1614 PyObject *result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 pyus_in = delta_to_microseconds(delta);
1617 if (pyus_in == NULL)
1618 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 pyus_out = PyNumber_FloorDivide(pyus_in, intobj);
1621 Py_DECREF(pyus_in);
1622 if (pyus_out == NULL)
1623 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 result = microseconds_to_delta(pyus_out);
1626 Py_DECREF(pyus_out);
1627 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001628}
1629
1630static PyObject *
Mark Dickinson7c186e22010-04-20 22:32:49 +00001631divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right)
1632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 PyObject *pyus_left;
1634 PyObject *pyus_right;
1635 PyObject *result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 pyus_left = delta_to_microseconds(left);
1638 if (pyus_left == NULL)
1639 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 pyus_right = delta_to_microseconds(right);
1642 if (pyus_right == NULL) {
1643 Py_DECREF(pyus_left);
1644 return NULL;
1645 }
Mark Dickinson7c186e22010-04-20 22:32:49 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 result = PyNumber_FloorDivide(pyus_left, pyus_right);
1648 Py_DECREF(pyus_left);
1649 Py_DECREF(pyus_right);
1650 return result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001651}
1652
1653static PyObject *
1654truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right)
1655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 PyObject *pyus_left;
1657 PyObject *pyus_right;
1658 PyObject *result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 pyus_left = delta_to_microseconds(left);
1661 if (pyus_left == NULL)
1662 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 pyus_right = delta_to_microseconds(right);
1665 if (pyus_right == NULL) {
1666 Py_DECREF(pyus_left);
1667 return NULL;
1668 }
Mark Dickinson7c186e22010-04-20 22:32:49 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 result = PyNumber_TrueDivide(pyus_left, pyus_right);
1671 Py_DECREF(pyus_left);
1672 Py_DECREF(pyus_right);
1673 return result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001674}
1675
1676static PyObject *
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001677truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f)
1678{
1679 PyObject *result = NULL;
1680 PyObject *pyus_in = NULL, *temp, *pyus_out;
1681 PyObject *ratio = NULL;
1682
1683 pyus_in = delta_to_microseconds(delta);
1684 if (pyus_in == NULL)
1685 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001686 ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL);
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001687 if (ratio == NULL)
1688 goto error;
1689 temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1));
1690 Py_DECREF(pyus_in);
1691 pyus_in = NULL;
1692 if (temp == NULL)
1693 goto error;
1694 pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0));
1695 Py_DECREF(temp);
1696 if (pyus_out == NULL)
1697 goto error;
1698 result = microseconds_to_delta(pyus_out);
1699 Py_DECREF(pyus_out);
1700 error:
1701 Py_XDECREF(pyus_in);
1702 Py_XDECREF(ratio);
1703
1704 return result;
1705}
1706
1707static PyObject *
1708truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i)
1709{
1710 PyObject *result;
1711 PyObject *pyus_in, *pyus_out;
1712 pyus_in = delta_to_microseconds(delta);
1713 if (pyus_in == NULL)
1714 return NULL;
1715 pyus_out = divide_nearest(pyus_in, i);
1716 Py_DECREF(pyus_in);
1717 if (pyus_out == NULL)
1718 return NULL;
1719 result = microseconds_to_delta(pyus_out);
1720 Py_DECREF(pyus_out);
1721
1722 return result;
1723}
1724
1725static PyObject *
Tim Peters2a799bf2002-12-16 20:18:38 +00001726delta_add(PyObject *left, PyObject *right)
1727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 PyObject *result = Py_NotImplemented;
Tim Peters2a799bf2002-12-16 20:18:38 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 if (PyDelta_Check(left) && PyDelta_Check(right)) {
1731 /* delta + delta */
1732 /* The C-level additions can't overflow because of the
1733 * invariant bounds.
1734 */
1735 int days = GET_TD_DAYS(left) + GET_TD_DAYS(right);
1736 int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right);
1737 int microseconds = GET_TD_MICROSECONDS(left) +
1738 GET_TD_MICROSECONDS(right);
1739 result = new_delta(days, seconds, microseconds, 1);
1740 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (result == Py_NotImplemented)
1743 Py_INCREF(result);
1744 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001745}
1746
1747static PyObject *
1748delta_negative(PyDateTime_Delta *self)
1749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 return new_delta(-GET_TD_DAYS(self),
1751 -GET_TD_SECONDS(self),
1752 -GET_TD_MICROSECONDS(self),
1753 1);
Tim Peters2a799bf2002-12-16 20:18:38 +00001754}
1755
1756static PyObject *
1757delta_positive(PyDateTime_Delta *self)
1758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 /* Could optimize this (by returning self) if this isn't a
1760 * subclass -- but who uses unary + ? Approximately nobody.
1761 */
1762 return new_delta(GET_TD_DAYS(self),
1763 GET_TD_SECONDS(self),
1764 GET_TD_MICROSECONDS(self),
1765 0);
Tim Peters2a799bf2002-12-16 20:18:38 +00001766}
1767
1768static PyObject *
1769delta_abs(PyDateTime_Delta *self)
1770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 PyObject *result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 assert(GET_TD_MICROSECONDS(self) >= 0);
1774 assert(GET_TD_SECONDS(self) >= 0);
Tim Peters2a799bf2002-12-16 20:18:38 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (GET_TD_DAYS(self) < 0)
1777 result = delta_negative(self);
1778 else
1779 result = delta_positive(self);
Tim Peters2a799bf2002-12-16 20:18:38 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001782}
1783
1784static PyObject *
1785delta_subtract(PyObject *left, PyObject *right)
1786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 PyObject *result = Py_NotImplemented;
Tim Peters2a799bf2002-12-16 20:18:38 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (PyDelta_Check(left) && PyDelta_Check(right)) {
1790 /* delta - delta */
Alexander Belopolskyb6f5ec72011-04-05 20:07:38 -04001791 /* The C-level additions can't overflow because of the
1792 * invariant bounds.
1793 */
1794 int days = GET_TD_DAYS(left) - GET_TD_DAYS(right);
1795 int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right);
1796 int microseconds = GET_TD_MICROSECONDS(left) -
1797 GET_TD_MICROSECONDS(right);
1798 result = new_delta(days, seconds, microseconds, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (result == Py_NotImplemented)
1802 Py_INCREF(result);
1803 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001804}
1805
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001806static int
1807delta_cmp(PyObject *self, PyObject *other)
1808{
1809 int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
1810 if (diff == 0) {
1811 diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
1812 if (diff == 0)
1813 diff = GET_TD_MICROSECONDS(self) -
1814 GET_TD_MICROSECONDS(other);
1815 }
1816 return diff;
1817}
1818
Tim Peters2a799bf2002-12-16 20:18:38 +00001819static PyObject *
Guido van Rossum19960592006-08-24 17:29:38 +00001820delta_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters2a799bf2002-12-16 20:18:38 +00001821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (PyDelta_Check(other)) {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00001823 int diff = delta_cmp(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 return diff_to_bool(diff, op);
1825 }
1826 else {
Brian Curtindfc80e32011-08-10 20:28:54 -05001827 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001829}
1830
1831static PyObject *delta_getstate(PyDateTime_Delta *self);
1832
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001833static Py_hash_t
Tim Peters2a799bf2002-12-16 20:18:38 +00001834delta_hash(PyDateTime_Delta *self)
1835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (self->hashcode == -1) {
1837 PyObject *temp = delta_getstate(self);
1838 if (temp != NULL) {
1839 self->hashcode = PyObject_Hash(temp);
1840 Py_DECREF(temp);
1841 }
1842 }
1843 return self->hashcode;
Tim Peters2a799bf2002-12-16 20:18:38 +00001844}
1845
1846static PyObject *
1847delta_multiply(PyObject *left, PyObject *right)
1848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 PyObject *result = Py_NotImplemented;
Tim Peters2a799bf2002-12-16 20:18:38 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 if (PyDelta_Check(left)) {
1852 /* delta * ??? */
1853 if (PyLong_Check(right))
1854 result = multiply_int_timedelta(right,
1855 (PyDateTime_Delta *) left);
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001856 else if (PyFloat_Check(right))
1857 result = multiply_float_timedelta(right,
1858 (PyDateTime_Delta *) left);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 }
1860 else if (PyLong_Check(left))
1861 result = multiply_int_timedelta(left,
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001862 (PyDateTime_Delta *) right);
1863 else if (PyFloat_Check(left))
1864 result = multiply_float_timedelta(left,
1865 (PyDateTime_Delta *) right);
Tim Peters2a799bf2002-12-16 20:18:38 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 if (result == Py_NotImplemented)
1868 Py_INCREF(result);
1869 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001870}
1871
1872static PyObject *
1873delta_divide(PyObject *left, PyObject *right)
1874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 PyObject *result = Py_NotImplemented;
Tim Peters2a799bf2002-12-16 20:18:38 +00001876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 if (PyDelta_Check(left)) {
1878 /* delta * ??? */
1879 if (PyLong_Check(right))
1880 result = divide_timedelta_int(
1881 (PyDateTime_Delta *)left,
1882 right);
1883 else if (PyDelta_Check(right))
1884 result = divide_timedelta_timedelta(
1885 (PyDateTime_Delta *)left,
1886 (PyDateTime_Delta *)right);
1887 }
Tim Peters2a799bf2002-12-16 20:18:38 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (result == Py_NotImplemented)
1890 Py_INCREF(result);
1891 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00001892}
1893
Mark Dickinson7c186e22010-04-20 22:32:49 +00001894static PyObject *
1895delta_truedivide(PyObject *left, PyObject *right)
1896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 PyObject *result = Py_NotImplemented;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 if (PyDelta_Check(left)) {
1900 if (PyDelta_Check(right))
1901 result = truedivide_timedelta_timedelta(
1902 (PyDateTime_Delta *)left,
1903 (PyDateTime_Delta *)right);
Alexander Belopolsky1790bc42010-05-31 17:33:47 +00001904 else if (PyFloat_Check(right))
1905 result = truedivide_timedelta_float(
1906 (PyDateTime_Delta *)left, right);
1907 else if (PyLong_Check(right))
1908 result = truedivide_timedelta_int(
1909 (PyDateTime_Delta *)left, right);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 }
Mark Dickinson7c186e22010-04-20 22:32:49 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (result == Py_NotImplemented)
1913 Py_INCREF(result);
1914 return result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001915}
1916
1917static PyObject *
1918delta_remainder(PyObject *left, PyObject *right)
1919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 PyObject *pyus_left;
1921 PyObject *pyus_right;
1922 PyObject *pyus_remainder;
1923 PyObject *remainder;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001924
Brian Curtindfc80e32011-08-10 20:28:54 -05001925 if (!PyDelta_Check(left) || !PyDelta_Check(right))
1926 Py_RETURN_NOTIMPLEMENTED;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 pyus_left = delta_to_microseconds((PyDateTime_Delta *)left);
1929 if (pyus_left == NULL)
1930 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 pyus_right = delta_to_microseconds((PyDateTime_Delta *)right);
1933 if (pyus_right == NULL) {
1934 Py_DECREF(pyus_left);
1935 return NULL;
1936 }
Mark Dickinson7c186e22010-04-20 22:32:49 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right);
1939 Py_DECREF(pyus_left);
1940 Py_DECREF(pyus_right);
1941 if (pyus_remainder == NULL)
1942 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 remainder = microseconds_to_delta(pyus_remainder);
1945 Py_DECREF(pyus_remainder);
1946 if (remainder == NULL)
1947 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return remainder;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001950}
1951
1952static PyObject *
1953delta_divmod(PyObject *left, PyObject *right)
1954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 PyObject *pyus_left;
1956 PyObject *pyus_right;
1957 PyObject *divmod;
1958 PyObject *delta;
1959 PyObject *result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001960
Brian Curtindfc80e32011-08-10 20:28:54 -05001961 if (!PyDelta_Check(left) || !PyDelta_Check(right))
1962 Py_RETURN_NOTIMPLEMENTED;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 pyus_left = delta_to_microseconds((PyDateTime_Delta *)left);
1965 if (pyus_left == NULL)
1966 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 pyus_right = delta_to_microseconds((PyDateTime_Delta *)right);
1969 if (pyus_right == NULL) {
1970 Py_DECREF(pyus_left);
1971 return NULL;
1972 }
Mark Dickinson7c186e22010-04-20 22:32:49 +00001973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 divmod = PyNumber_Divmod(pyus_left, pyus_right);
1975 Py_DECREF(pyus_left);
1976 Py_DECREF(pyus_right);
1977 if (divmod == NULL)
1978 return NULL;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 assert(PyTuple_Size(divmod) == 2);
1981 delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1));
1982 if (delta == NULL) {
1983 Py_DECREF(divmod);
1984 return NULL;
1985 }
1986 result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta);
1987 Py_DECREF(delta);
1988 Py_DECREF(divmod);
1989 return result;
Mark Dickinson7c186e22010-04-20 22:32:49 +00001990}
1991
Tim Peters2a799bf2002-12-16 20:18:38 +00001992/* Fold in the value of the tag ("seconds", "weeks", etc) component of a
1993 * timedelta constructor. sofar is the # of microseconds accounted for
1994 * so far, and there are factor microseconds per current unit, the number
1995 * of which is given by num. num * factor is added to sofar in a
1996 * numerically careful way, and that's the result. Any fractional
1997 * microseconds left over (this can happen if num is a float type) are
1998 * added into *leftover.
1999 * Note that there are many ways this can give an error (NULL) return.
2000 */
2001static PyObject *
2002accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
2003 double *leftover)
2004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 PyObject *prod;
2006 PyObject *sum;
Tim Peters2a799bf2002-12-16 20:18:38 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 assert(num != NULL);
Tim Peters2a799bf2002-12-16 20:18:38 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 if (PyLong_Check(num)) {
2011 prod = PyNumber_Multiply(num, factor);
2012 if (prod == NULL)
2013 return NULL;
2014 sum = PyNumber_Add(sofar, prod);
2015 Py_DECREF(prod);
2016 return sum;
2017 }
Tim Peters2a799bf2002-12-16 20:18:38 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 if (PyFloat_Check(num)) {
2020 double dnum;
2021 double fracpart;
2022 double intpart;
2023 PyObject *x;
2024 PyObject *y;
Tim Peters2a799bf2002-12-16 20:18:38 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 /* The Plan: decompose num into an integer part and a
2027 * fractional part, num = intpart + fracpart.
2028 * Then num * factor ==
2029 * intpart * factor + fracpart * factor
2030 * and the LHS can be computed exactly in long arithmetic.
2031 * The RHS is again broken into an int part and frac part.
2032 * and the frac part is added into *leftover.
2033 */
2034 dnum = PyFloat_AsDouble(num);
2035 if (dnum == -1.0 && PyErr_Occurred())
2036 return NULL;
2037 fracpart = modf(dnum, &intpart);
2038 x = PyLong_FromDouble(intpart);
2039 if (x == NULL)
2040 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 prod = PyNumber_Multiply(x, factor);
2043 Py_DECREF(x);
2044 if (prod == NULL)
2045 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 sum = PyNumber_Add(sofar, prod);
2048 Py_DECREF(prod);
2049 if (sum == NULL)
2050 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 if (fracpart == 0.0)
2053 return sum;
2054 /* So far we've lost no information. Dealing with the
2055 * fractional part requires float arithmetic, and may
2056 * lose a little info.
2057 */
2058 assert(PyLong_Check(factor));
2059 dnum = PyLong_AsDouble(factor);
Tim Peters2a799bf2002-12-16 20:18:38 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 dnum *= fracpart;
2062 fracpart = modf(dnum, &intpart);
2063 x = PyLong_FromDouble(intpart);
2064 if (x == NULL) {
2065 Py_DECREF(sum);
2066 return NULL;
2067 }
Tim Peters2a799bf2002-12-16 20:18:38 +00002068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 y = PyNumber_Add(sum, x);
2070 Py_DECREF(sum);
2071 Py_DECREF(x);
2072 *leftover += fracpart;
2073 return y;
2074 }
Tim Peters2a799bf2002-12-16 20:18:38 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 PyErr_Format(PyExc_TypeError,
2077 "unsupported type for timedelta %s component: %s",
2078 tag, Py_TYPE(num)->tp_name);
2079 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002080}
2081
2082static PyObject *
2083delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
2084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 PyObject *self = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 /* Argument objects. */
2088 PyObject *day = NULL;
2089 PyObject *second = NULL;
2090 PyObject *us = NULL;
2091 PyObject *ms = NULL;
2092 PyObject *minute = NULL;
2093 PyObject *hour = NULL;
2094 PyObject *week = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 PyObject *x = NULL; /* running sum of microseconds */
2097 PyObject *y = NULL; /* temp sum of microseconds */
2098 double leftover_us = 0.0;
Tim Peters2a799bf2002-12-16 20:18:38 +00002099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 static char *keywords[] = {
2101 "days", "seconds", "microseconds", "milliseconds",
2102 "minutes", "hours", "weeks", NULL
2103 };
Tim Peters2a799bf2002-12-16 20:18:38 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__",
2106 keywords,
2107 &day, &second, &us,
2108 &ms, &minute, &hour, &week) == 0)
2109 goto Done;
Tim Peters2a799bf2002-12-16 20:18:38 +00002110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 x = PyLong_FromLong(0);
2112 if (x == NULL)
2113 goto Done;
Tim Peters2a799bf2002-12-16 20:18:38 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115#define CLEANUP \
2116 Py_DECREF(x); \
2117 x = y; \
2118 if (x == NULL) \
2119 goto Done
Tim Peters2a799bf2002-12-16 20:18:38 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 if (us) {
Alexander Belopolsky790d2692013-08-04 14:51:35 -04002122 y = accum("microseconds", x, us, one, &leftover_us);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 CLEANUP;
2124 }
2125 if (ms) {
2126 y = accum("milliseconds", x, ms, us_per_ms, &leftover_us);
2127 CLEANUP;
2128 }
2129 if (second) {
2130 y = accum("seconds", x, second, us_per_second, &leftover_us);
2131 CLEANUP;
2132 }
2133 if (minute) {
2134 y = accum("minutes", x, minute, us_per_minute, &leftover_us);
2135 CLEANUP;
2136 }
2137 if (hour) {
2138 y = accum("hours", x, hour, us_per_hour, &leftover_us);
2139 CLEANUP;
2140 }
2141 if (day) {
2142 y = accum("days", x, day, us_per_day, &leftover_us);
2143 CLEANUP;
2144 }
2145 if (week) {
2146 y = accum("weeks", x, week, us_per_week, &leftover_us);
2147 CLEANUP;
2148 }
2149 if (leftover_us) {
2150 /* Round to nearest whole # of us, and add into x. */
Alexander Belopolsky790d2692013-08-04 14:51:35 -04002151 double whole_us = round(leftover_us);
2152 int x_is_odd;
2153 PyObject *temp;
2154
2155 whole_us = round(leftover_us);
2156 if (fabs(whole_us - leftover_us) == 0.5) {
2157 /* We're exactly halfway between two integers. In order
2158 * to do round-half-to-even, we must determine whether x
2159 * is odd. Note that x is odd when it's last bit is 1. The
2160 * code below uses bitwise and operation to check the last
2161 * bit. */
Raymond Hettinger5a2146a2014-07-25 14:59:48 -07002162 temp = PyNumber_And(x, one); /* temp <- x & 1 */
Alexander Belopolsky790d2692013-08-04 14:51:35 -04002163 if (temp == NULL) {
2164 Py_DECREF(x);
2165 goto Done;
2166 }
2167 x_is_odd = PyObject_IsTrue(temp);
2168 Py_DECREF(temp);
2169 if (x_is_odd == -1) {
2170 Py_DECREF(x);
2171 goto Done;
2172 }
2173 whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd;
2174 }
2175
Victor Stinner36a5a062013-08-28 01:53:39 +02002176 temp = PyLong_FromLong((long)whole_us);
Alexander Belopolsky790d2692013-08-04 14:51:35 -04002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 if (temp == NULL) {
2179 Py_DECREF(x);
2180 goto Done;
2181 }
2182 y = PyNumber_Add(x, temp);
2183 Py_DECREF(temp);
2184 CLEANUP;
2185 }
Tim Peters2a799bf2002-12-16 20:18:38 +00002186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 self = microseconds_to_delta_ex(x, type);
2188 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00002189Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 return self;
Tim Peters2a799bf2002-12-16 20:18:38 +00002191
2192#undef CLEANUP
2193}
2194
2195static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00002196delta_bool(PyDateTime_Delta *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00002197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 return (GET_TD_DAYS(self) != 0
2199 || GET_TD_SECONDS(self) != 0
2200 || GET_TD_MICROSECONDS(self) != 0);
Tim Peters2a799bf2002-12-16 20:18:38 +00002201}
2202
2203static PyObject *
2204delta_repr(PyDateTime_Delta *self)
2205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (GET_TD_MICROSECONDS(self) != 0)
2207 return PyUnicode_FromFormat("%s(%d, %d, %d)",
2208 Py_TYPE(self)->tp_name,
2209 GET_TD_DAYS(self),
2210 GET_TD_SECONDS(self),
2211 GET_TD_MICROSECONDS(self));
2212 if (GET_TD_SECONDS(self) != 0)
2213 return PyUnicode_FromFormat("%s(%d, %d)",
2214 Py_TYPE(self)->tp_name,
2215 GET_TD_DAYS(self),
2216 GET_TD_SECONDS(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 return PyUnicode_FromFormat("%s(%d)",
2219 Py_TYPE(self)->tp_name,
2220 GET_TD_DAYS(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002221}
2222
2223static PyObject *
2224delta_str(PyDateTime_Delta *self)
2225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 int us = GET_TD_MICROSECONDS(self);
2227 int seconds = GET_TD_SECONDS(self);
2228 int minutes = divmod(seconds, 60, &seconds);
2229 int hours = divmod(minutes, 60, &minutes);
2230 int days = GET_TD_DAYS(self);
Tim Peters2a799bf2002-12-16 20:18:38 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 if (days) {
2233 if (us)
2234 return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d",
2235 days, (days == 1 || days == -1) ? "" : "s",
2236 hours, minutes, seconds, us);
2237 else
2238 return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d",
2239 days, (days == 1 || days == -1) ? "" : "s",
2240 hours, minutes, seconds);
2241 } else {
2242 if (us)
2243 return PyUnicode_FromFormat("%d:%02d:%02d.%06d",
2244 hours, minutes, seconds, us);
2245 else
2246 return PyUnicode_FromFormat("%d:%02d:%02d",
2247 hours, minutes, seconds);
2248 }
Tim Peters2a799bf2002-12-16 20:18:38 +00002249
Tim Peters2a799bf2002-12-16 20:18:38 +00002250}
2251
Tim Peters371935f2003-02-01 01:52:50 +00002252/* Pickle support, a simple use of __reduce__. */
2253
Tim Petersb57f8f02003-02-01 02:54:15 +00002254/* __getstate__ isn't exposed */
Tim Peters2a799bf2002-12-16 20:18:38 +00002255static PyObject *
2256delta_getstate(PyDateTime_Delta *self)
2257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 return Py_BuildValue("iii", GET_TD_DAYS(self),
2259 GET_TD_SECONDS(self),
2260 GET_TD_MICROSECONDS(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002261}
2262
Tim Peters2a799bf2002-12-16 20:18:38 +00002263static PyObject *
Antoine Pitroube6859d2009-11-25 23:02:32 +00002264delta_total_seconds(PyObject *self)
2265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 PyObject *total_seconds;
2267 PyObject *total_microseconds;
Mark Dickinson0381e3f2010-05-08 14:35:02 +00002268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self);
2270 if (total_microseconds == NULL)
2271 return NULL;
Mark Dickinson0381e3f2010-05-08 14:35:02 +00002272
Alexander Belopolskydf7027b2013-08-04 15:18:58 -04002273 total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second);
Mark Dickinson0381e3f2010-05-08 14:35:02 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 Py_DECREF(total_microseconds);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 return total_seconds;
Antoine Pitroube6859d2009-11-25 23:02:32 +00002277}
2278
2279static PyObject *
Tim Peters2a799bf2002-12-16 20:18:38 +00002280delta_reduce(PyDateTime_Delta* self)
2281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002283}
2284
2285#define OFFSET(field) offsetof(PyDateTime_Delta, field)
2286
2287static PyMemberDef delta_members[] = {
Guido van Rossum177e41a2003-01-30 22:06:23 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 {"days", T_INT, OFFSET(days), READONLY,
2290 PyDoc_STR("Number of days.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 {"seconds", T_INT, OFFSET(seconds), READONLY,
2293 PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 {"microseconds", T_INT, OFFSET(microseconds), READONLY,
2296 PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")},
2297 {NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00002298};
2299
2300static PyMethodDef delta_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS,
2302 PyDoc_STR("Total seconds in the duration.")},
Antoine Pitroube6859d2009-11-25 23:02:32 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS,
2305 PyDoc_STR("__reduce__() -> (cls, state)")},
Guido van Rossum177e41a2003-01-30 22:06:23 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 {NULL, NULL},
Tim Peters2a799bf2002-12-16 20:18:38 +00002308};
2309
2310static char delta_doc[] =
2311PyDoc_STR("Difference between two datetime values.");
2312
2313static PyNumberMethods delta_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 delta_add, /* nb_add */
2315 delta_subtract, /* nb_subtract */
2316 delta_multiply, /* nb_multiply */
2317 delta_remainder, /* nb_remainder */
2318 delta_divmod, /* nb_divmod */
2319 0, /* nb_power */
2320 (unaryfunc)delta_negative, /* nb_negative */
2321 (unaryfunc)delta_positive, /* nb_positive */
2322 (unaryfunc)delta_abs, /* nb_absolute */
2323 (inquiry)delta_bool, /* nb_bool */
2324 0, /*nb_invert*/
2325 0, /*nb_lshift*/
2326 0, /*nb_rshift*/
2327 0, /*nb_and*/
2328 0, /*nb_xor*/
2329 0, /*nb_or*/
2330 0, /*nb_int*/
2331 0, /*nb_reserved*/
2332 0, /*nb_float*/
2333 0, /*nb_inplace_add*/
2334 0, /*nb_inplace_subtract*/
2335 0, /*nb_inplace_multiply*/
2336 0, /*nb_inplace_remainder*/
2337 0, /*nb_inplace_power*/
2338 0, /*nb_inplace_lshift*/
2339 0, /*nb_inplace_rshift*/
2340 0, /*nb_inplace_and*/
2341 0, /*nb_inplace_xor*/
2342 0, /*nb_inplace_or*/
2343 delta_divide, /* nb_floor_divide */
2344 delta_truedivide, /* nb_true_divide */
2345 0, /* nb_inplace_floor_divide */
2346 0, /* nb_inplace_true_divide */
Tim Peters2a799bf2002-12-16 20:18:38 +00002347};
2348
2349static PyTypeObject PyDateTime_DeltaType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 PyVarObject_HEAD_INIT(NULL, 0)
2351 "datetime.timedelta", /* tp_name */
2352 sizeof(PyDateTime_Delta), /* tp_basicsize */
2353 0, /* tp_itemsize */
2354 0, /* tp_dealloc */
2355 0, /* tp_print */
2356 0, /* tp_getattr */
2357 0, /* tp_setattr */
2358 0, /* tp_reserved */
2359 (reprfunc)delta_repr, /* tp_repr */
2360 &delta_as_number, /* tp_as_number */
2361 0, /* tp_as_sequence */
2362 0, /* tp_as_mapping */
2363 (hashfunc)delta_hash, /* tp_hash */
2364 0, /* tp_call */
2365 (reprfunc)delta_str, /* tp_str */
2366 PyObject_GenericGetAttr, /* tp_getattro */
2367 0, /* tp_setattro */
2368 0, /* tp_as_buffer */
2369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2370 delta_doc, /* tp_doc */
2371 0, /* tp_traverse */
2372 0, /* tp_clear */
2373 delta_richcompare, /* tp_richcompare */
2374 0, /* tp_weaklistoffset */
2375 0, /* tp_iter */
2376 0, /* tp_iternext */
2377 delta_methods, /* tp_methods */
2378 delta_members, /* tp_members */
2379 0, /* tp_getset */
2380 0, /* tp_base */
2381 0, /* tp_dict */
2382 0, /* tp_descr_get */
2383 0, /* tp_descr_set */
2384 0, /* tp_dictoffset */
2385 0, /* tp_init */
2386 0, /* tp_alloc */
2387 delta_new, /* tp_new */
2388 0, /* tp_free */
Tim Peters2a799bf2002-12-16 20:18:38 +00002389};
2390
2391/*
2392 * PyDateTime_Date implementation.
2393 */
2394
2395/* Accessor properties. */
2396
2397static PyObject *
2398date_year(PyDateTime_Date *self, void *unused)
2399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 return PyLong_FromLong(GET_YEAR(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002401}
2402
2403static PyObject *
2404date_month(PyDateTime_Date *self, void *unused)
2405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 return PyLong_FromLong(GET_MONTH(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002407}
2408
2409static PyObject *
2410date_day(PyDateTime_Date *self, void *unused)
2411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 return PyLong_FromLong(GET_DAY(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002413}
2414
2415static PyGetSetDef date_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 {"year", (getter)date_year},
2417 {"month", (getter)date_month},
2418 {"day", (getter)date_day},
2419 {NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00002420};
2421
2422/* Constructors. */
2423
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +00002424static char *date_kws[] = {"year", "month", "day", NULL};
Tim Peters12bf3392002-12-24 05:41:27 +00002425
Tim Peters2a799bf2002-12-16 20:18:38 +00002426static PyObject *
2427date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
2428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 PyObject *self = NULL;
2430 PyObject *state;
2431 int year;
2432 int month;
2433 int day;
Tim Peters2a799bf2002-12-16 20:18:38 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Check for invocation from pickle with __getstate__ state */
2436 if (PyTuple_GET_SIZE(args) == 1 &&
2437 PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
2438 PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
2439 MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
2440 {
2441 PyDateTime_Date *me;
Tim Peters70533e22003-02-01 04:40:04 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
2444 if (me != NULL) {
2445 char *pdata = PyBytes_AS_STRING(state);
2446 memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
2447 me->hashcode = -1;
2448 }
2449 return (PyObject *)me;
2450 }
Guido van Rossum177e41a2003-01-30 22:06:23 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
2453 &year, &month, &day)) {
2454 if (check_date_args(year, month, day) < 0)
2455 return NULL;
2456 self = new_date_ex(year, month, day, type);
2457 }
2458 return self;
Tim Peters2a799bf2002-12-16 20:18:38 +00002459}
2460
2461/* Return new date from localtime(t). */
2462static PyObject *
Victor Stinner5d272cc2012-03-13 13:35:55 +01002463date_local_from_object(PyObject *cls, PyObject *obj)
Tim Peters2a799bf2002-12-16 20:18:38 +00002464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 struct tm *tm;
2466 time_t t;
Tim Peters2a799bf2002-12-16 20:18:38 +00002467
Victor Stinnere4a994d2015-03-30 01:10:14 +02002468 if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 return NULL;
Victor Stinner5d272cc2012-03-13 13:35:55 +01002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 tm = localtime(&t);
Victor Stinner21f58932012-03-14 00:15:40 +01002472 if (tm == NULL) {
2473 /* unconvertible time */
2474#ifdef EINVAL
2475 if (errno == 0)
2476 errno = EINVAL;
2477#endif
2478 PyErr_SetFromErrno(PyExc_OSError);
2479 return NULL;
2480 }
2481
2482 return PyObject_CallFunction(cls, "iii",
2483 tm->tm_year + 1900,
2484 tm->tm_mon + 1,
2485 tm->tm_mday);
Tim Peters2a799bf2002-12-16 20:18:38 +00002486}
2487
2488/* Return new date from current time.
2489 * We say this is equivalent to fromtimestamp(time.time()), and the
2490 * only way to be sure of that is to *call* time.time(). That's not
2491 * generally the same as calling C's time.
2492 */
2493static PyObject *
2494date_today(PyObject *cls, PyObject *dummy)
2495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 PyObject *time;
2497 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002498 _Py_IDENTIFIER(fromtimestamp);
Tim Peters2a799bf2002-12-16 20:18:38 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 time = time_time();
2501 if (time == NULL)
2502 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 /* Note well: today() is a class method, so this may not call
2505 * date.fromtimestamp. For example, it may call
2506 * datetime.fromtimestamp. That's why we need all the accuracy
2507 * time.time() delivers; if someone were gonzo about optimization,
2508 * date.today() could get away with plain C time().
2509 */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002510 result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 Py_DECREF(time);
2512 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00002513}
2514
2515/* Return new date from given timestamp (Python timestamp -- a double). */
2516static PyObject *
2517date_fromtimestamp(PyObject *cls, PyObject *args)
2518{
Victor Stinner5d272cc2012-03-13 13:35:55 +01002519 PyObject *timestamp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002521
Victor Stinner5d272cc2012-03-13 13:35:55 +01002522 if (PyArg_ParseTuple(args, "O:fromtimestamp", &timestamp))
2523 result = date_local_from_object(cls, timestamp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00002525}
2526
2527/* Return new date from proleptic Gregorian ordinal. Raises ValueError if
2528 * the ordinal is out of range.
2529 */
2530static PyObject *
2531date_fromordinal(PyObject *cls, PyObject *args)
2532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 PyObject *result = NULL;
2534 int ordinal;
Tim Peters2a799bf2002-12-16 20:18:38 +00002535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) {
2537 int year;
2538 int month;
2539 int day;
Tim Peters2a799bf2002-12-16 20:18:38 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 if (ordinal < 1)
2542 PyErr_SetString(PyExc_ValueError, "ordinal must be "
2543 ">= 1");
2544 else {
2545 ord_to_ymd(ordinal, &year, &month, &day);
2546 result = PyObject_CallFunction(cls, "iii",
2547 year, month, day);
2548 }
2549 }
2550 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00002551}
2552
2553/*
2554 * Date arithmetic.
2555 */
2556
2557/* date + timedelta -> date. If arg negate is true, subtract the timedelta
2558 * instead.
2559 */
2560static PyObject *
2561add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate)
2562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 PyObject *result = NULL;
2564 int year = GET_YEAR(date);
2565 int month = GET_MONTH(date);
2566 int deltadays = GET_TD_DAYS(delta);
2567 /* C-level overflow is impossible because |deltadays| < 1e9. */
2568 int day = GET_DAY(date) + (negate ? -deltadays : deltadays);
Tim Peters2a799bf2002-12-16 20:18:38 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 if (normalize_date(&year, &month, &day) >= 0)
2571 result = new_date(year, month, day);
2572 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00002573}
2574
2575static PyObject *
2576date_add(PyObject *left, PyObject *right)
2577{
Brian Curtindfc80e32011-08-10 20:28:54 -05002578 if (PyDateTime_Check(left) || PyDateTime_Check(right))
2579 Py_RETURN_NOTIMPLEMENTED;
2580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 if (PyDate_Check(left)) {
2582 /* date + ??? */
2583 if (PyDelta_Check(right))
2584 /* date + delta */
2585 return add_date_timedelta((PyDateTime_Date *) left,
2586 (PyDateTime_Delta *) right,
2587 0);
2588 }
2589 else {
2590 /* ??? + date
2591 * 'right' must be one of us, or we wouldn't have been called
2592 */
2593 if (PyDelta_Check(left))
2594 /* delta + date */
2595 return add_date_timedelta((PyDateTime_Date *) right,
2596 (PyDateTime_Delta *) left,
2597 0);
2598 }
Brian Curtindfc80e32011-08-10 20:28:54 -05002599 Py_RETURN_NOTIMPLEMENTED;
Tim Peters2a799bf2002-12-16 20:18:38 +00002600}
2601
2602static PyObject *
2603date_subtract(PyObject *left, PyObject *right)
2604{
Brian Curtindfc80e32011-08-10 20:28:54 -05002605 if (PyDateTime_Check(left) || PyDateTime_Check(right))
2606 Py_RETURN_NOTIMPLEMENTED;
2607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 if (PyDate_Check(left)) {
2609 if (PyDate_Check(right)) {
2610 /* date - date */
2611 int left_ord = ymd_to_ord(GET_YEAR(left),
2612 GET_MONTH(left),
2613 GET_DAY(left));
2614 int right_ord = ymd_to_ord(GET_YEAR(right),
2615 GET_MONTH(right),
2616 GET_DAY(right));
2617 return new_delta(left_ord - right_ord, 0, 0, 0);
2618 }
2619 if (PyDelta_Check(right)) {
2620 /* date - delta */
2621 return add_date_timedelta((PyDateTime_Date *) left,
2622 (PyDateTime_Delta *) right,
2623 1);
2624 }
2625 }
Brian Curtindfc80e32011-08-10 20:28:54 -05002626 Py_RETURN_NOTIMPLEMENTED;
Tim Peters2a799bf2002-12-16 20:18:38 +00002627}
2628
2629
2630/* Various ways to turn a date into a string. */
2631
2632static PyObject *
2633date_repr(PyDateTime_Date *self)
2634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 return PyUnicode_FromFormat("%s(%d, %d, %d)",
2636 Py_TYPE(self)->tp_name,
2637 GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002638}
2639
2640static PyObject *
2641date_isoformat(PyDateTime_Date *self)
2642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 return PyUnicode_FromFormat("%04d-%02d-%02d",
2644 GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002645}
2646
Tim Peterse2df5ff2003-05-02 18:39:55 +00002647/* str() calls the appropriate isoformat() method. */
Tim Peters2a799bf2002-12-16 20:18:38 +00002648static PyObject *
2649date_str(PyDateTime_Date *self)
2650{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002651 return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()");
Tim Peters2a799bf2002-12-16 20:18:38 +00002652}
2653
2654
2655static PyObject *
2656date_ctime(PyDateTime_Date *self)
2657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 return format_ctime(self, 0, 0, 0);
Tim Peters2a799bf2002-12-16 20:18:38 +00002659}
2660
2661static PyObject *
2662date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw)
2663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 /* This method can be inherited, and needs to call the
2665 * timetuple() method appropriate to self's class.
2666 */
2667 PyObject *result;
2668 PyObject *tuple;
2669 PyObject *format;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002670 _Py_IDENTIFIER(timetuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 static char *keywords[] = {"format", NULL};
Tim Peters2a799bf2002-12-16 20:18:38 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
2674 &format))
2675 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002676
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002677 tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, "()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 if (tuple == NULL)
2679 return NULL;
2680 result = wrap_strftime((PyObject *)self, format, tuple,
2681 (PyObject *)self);
2682 Py_DECREF(tuple);
2683 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00002684}
2685
Eric Smith1ba31142007-09-11 18:06:02 +00002686static PyObject *
2687date_format(PyDateTime_Date *self, PyObject *args)
2688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 PyObject *format;
Eric Smith1ba31142007-09-11 18:06:02 +00002690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 if (!PyArg_ParseTuple(args, "U:__format__", &format))
2692 return NULL;
Eric Smith1ba31142007-09-11 18:06:02 +00002693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 /* if the format is zero length, return str(self) */
Victor Stinner9e30aa52011-11-21 02:49:52 +01002695 if (PyUnicode_GetLength(format) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 return PyObject_Str((PyObject *)self);
Eric Smith1ba31142007-09-11 18:06:02 +00002697
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002698 return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format);
Eric Smith1ba31142007-09-11 18:06:02 +00002699}
2700
Tim Peters2a799bf2002-12-16 20:18:38 +00002701/* ISO methods. */
2702
2703static PyObject *
2704date_isoweekday(PyDateTime_Date *self)
2705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 return PyLong_FromLong(dow + 1);
Tim Peters2a799bf2002-12-16 20:18:38 +00002709}
2710
2711static PyObject *
2712date_isocalendar(PyDateTime_Date *self)
2713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 int year = GET_YEAR(self);
2715 int week1_monday = iso_week1_monday(year);
2716 int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self));
2717 int week;
2718 int day;
Tim Peters2a799bf2002-12-16 20:18:38 +00002719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 week = divmod(today - week1_monday, 7, &day);
2721 if (week < 0) {
2722 --year;
2723 week1_monday = iso_week1_monday(year);
2724 week = divmod(today - week1_monday, 7, &day);
2725 }
2726 else if (week >= 52 && today >= iso_week1_monday(year + 1)) {
2727 ++year;
2728 week = 0;
2729 }
2730 return Py_BuildValue("iii", year, week + 1, day + 1);
Tim Peters2a799bf2002-12-16 20:18:38 +00002731}
2732
2733/* Miscellaneous methods. */
2734
Tim Peters2a799bf2002-12-16 20:18:38 +00002735static PyObject *
Guido van Rossum19960592006-08-24 17:29:38 +00002736date_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters2a799bf2002-12-16 20:18:38 +00002737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 if (PyDate_Check(other)) {
2739 int diff = memcmp(((PyDateTime_Date *)self)->data,
2740 ((PyDateTime_Date *)other)->data,
2741 _PyDateTime_DATE_DATASIZE);
2742 return diff_to_bool(diff, op);
2743 }
Brian Curtindfc80e32011-08-10 20:28:54 -05002744 else
2745 Py_RETURN_NOTIMPLEMENTED;
Tim Peters2a799bf2002-12-16 20:18:38 +00002746}
2747
2748static PyObject *
2749date_timetuple(PyDateTime_Date *self)
2750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 return build_struct_time(GET_YEAR(self),
2752 GET_MONTH(self),
2753 GET_DAY(self),
2754 0, 0, 0, -1);
Tim Peters2a799bf2002-12-16 20:18:38 +00002755}
2756
Tim Peters12bf3392002-12-24 05:41:27 +00002757static PyObject *
2758date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw)
2759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 PyObject *clone;
2761 PyObject *tuple;
2762 int year = GET_YEAR(self);
2763 int month = GET_MONTH(self);
2764 int day = GET_DAY(self);
Tim Peters12bf3392002-12-24 05:41:27 +00002765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
2767 &year, &month, &day))
2768 return NULL;
2769 tuple = Py_BuildValue("iii", year, month, day);
2770 if (tuple == NULL)
2771 return NULL;
2772 clone = date_new(Py_TYPE(self), tuple, NULL);
2773 Py_DECREF(tuple);
2774 return clone;
Tim Peters12bf3392002-12-24 05:41:27 +00002775}
2776
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002777static Py_hash_t
Guido van Rossumfd53fd62007-08-24 04:05:13 +00002778generic_hash(unsigned char *data, int len)
2779{
Gregory P. Smith5831bd22012-01-14 14:31:13 -08002780 return _Py_HashBytes(data, len);
Guido van Rossumfd53fd62007-08-24 04:05:13 +00002781}
2782
2783
2784static PyObject *date_getstate(PyDateTime_Date *self);
Tim Peters2a799bf2002-12-16 20:18:38 +00002785
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002786static Py_hash_t
Tim Peters2a799bf2002-12-16 20:18:38 +00002787date_hash(PyDateTime_Date *self)
2788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 if (self->hashcode == -1)
2790 self->hashcode = generic_hash(
2791 (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE);
Guido van Rossum254348e2007-11-21 19:29:53 +00002792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 return self->hashcode;
Tim Peters2a799bf2002-12-16 20:18:38 +00002794}
2795
2796static PyObject *
2797date_toordinal(PyDateTime_Date *self)
2798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self),
2800 GET_DAY(self)));
Tim Peters2a799bf2002-12-16 20:18:38 +00002801}
2802
2803static PyObject *
2804date_weekday(PyDateTime_Date *self)
2805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 return PyLong_FromLong(dow);
Tim Peters2a799bf2002-12-16 20:18:38 +00002809}
2810
Tim Peters371935f2003-02-01 01:52:50 +00002811/* Pickle support, a simple use of __reduce__. */
Tim Peters2a799bf2002-12-16 20:18:38 +00002812
Tim Petersb57f8f02003-02-01 02:54:15 +00002813/* __getstate__ isn't exposed */
Tim Peters2a799bf2002-12-16 20:18:38 +00002814static PyObject *
Guido van Rossumfd53fd62007-08-24 04:05:13 +00002815date_getstate(PyDateTime_Date *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00002816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 PyObject* field;
2818 field = PyBytes_FromStringAndSize((char*)self->data,
2819 _PyDateTime_DATE_DATASIZE);
2820 return Py_BuildValue("(N)", field);
Tim Peters2a799bf2002-12-16 20:18:38 +00002821}
2822
2823static PyObject *
Guido van Rossum177e41a2003-01-30 22:06:23 +00002824date_reduce(PyDateTime_Date *self, PyObject *arg)
Tim Peters2a799bf2002-12-16 20:18:38 +00002825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00002827}
2828
2829static PyMethodDef date_methods[] = {
Guido van Rossum177e41a2003-01-30 22:06:23 +00002830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 /* Class methods: */
Guido van Rossum177e41a2003-01-30 22:06:23 +00002832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS |
2834 METH_CLASS,
2835 PyDoc_STR("timestamp -> local date from a POSIX timestamp (like "
2836 "time.time()).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
2839 METH_CLASS,
2840 PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
2841 "ordinal.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS,
2844 PyDoc_STR("Current date or datetime: same as "
2845 "self.__class__.fromtimestamp(time.time()).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 /* Instance methods: */
Tim Peters2a799bf2002-12-16 20:18:38 +00002848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 {"ctime", (PyCFunction)date_ctime, METH_NOARGS,
2850 PyDoc_STR("Return ctime() style string.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS,
2853 PyDoc_STR("format -> strftime() style string.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 {"__format__", (PyCFunction)date_format, METH_VARARGS,
2856 PyDoc_STR("Formats self with strftime.")},
Eric Smith1ba31142007-09-11 18:06:02 +00002857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS,
2859 PyDoc_STR("Return time tuple, compatible with time.localtime().")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS,
2862 PyDoc_STR("Return a 3-tuple containing ISO year, week number, and "
2863 "weekday.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS,
2866 PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS,
2869 PyDoc_STR("Return the day of the week represented by the date.\n"
2870 "Monday == 1 ... Sunday == 7")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS,
2873 PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year "
2874 "1 is day 1.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 {"weekday", (PyCFunction)date_weekday, METH_NOARGS,
2877 PyDoc_STR("Return the day of the week represented by the date.\n"
2878 "Monday == 0 ... Sunday == 6")},
Tim Peters2a799bf2002-12-16 20:18:38 +00002879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS,
2881 PyDoc_STR("Return date with new specified fields.")},
Tim Peters12bf3392002-12-24 05:41:27 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS,
2884 PyDoc_STR("__reduce__() -> (cls, state)")},
Guido van Rossum177e41a2003-01-30 22:06:23 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 {NULL, NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00002887};
2888
2889static char date_doc[] =
Raymond Hettinger3a4231d2004-12-19 20:13:24 +00002890PyDoc_STR("date(year, month, day) --> date object");
Tim Peters2a799bf2002-12-16 20:18:38 +00002891
2892static PyNumberMethods date_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 date_add, /* nb_add */
2894 date_subtract, /* nb_subtract */
2895 0, /* nb_multiply */
2896 0, /* nb_remainder */
2897 0, /* nb_divmod */
2898 0, /* nb_power */
2899 0, /* nb_negative */
2900 0, /* nb_positive */
2901 0, /* nb_absolute */
2902 0, /* nb_bool */
Tim Peters2a799bf2002-12-16 20:18:38 +00002903};
2904
2905static PyTypeObject PyDateTime_DateType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 PyVarObject_HEAD_INIT(NULL, 0)
2907 "datetime.date", /* tp_name */
2908 sizeof(PyDateTime_Date), /* tp_basicsize */
2909 0, /* tp_itemsize */
2910 0, /* tp_dealloc */
2911 0, /* tp_print */
2912 0, /* tp_getattr */
2913 0, /* tp_setattr */
2914 0, /* tp_reserved */
2915 (reprfunc)date_repr, /* tp_repr */
2916 &date_as_number, /* tp_as_number */
2917 0, /* tp_as_sequence */
2918 0, /* tp_as_mapping */
2919 (hashfunc)date_hash, /* tp_hash */
2920 0, /* tp_call */
2921 (reprfunc)date_str, /* tp_str */
2922 PyObject_GenericGetAttr, /* tp_getattro */
2923 0, /* tp_setattro */
2924 0, /* tp_as_buffer */
2925 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2926 date_doc, /* tp_doc */
2927 0, /* tp_traverse */
2928 0, /* tp_clear */
2929 date_richcompare, /* tp_richcompare */
2930 0, /* tp_weaklistoffset */
2931 0, /* tp_iter */
2932 0, /* tp_iternext */
2933 date_methods, /* tp_methods */
2934 0, /* tp_members */
2935 date_getset, /* tp_getset */
2936 0, /* tp_base */
2937 0, /* tp_dict */
2938 0, /* tp_descr_get */
2939 0, /* tp_descr_set */
2940 0, /* tp_dictoffset */
2941 0, /* tp_init */
2942 0, /* tp_alloc */
2943 date_new, /* tp_new */
2944 0, /* tp_free */
Tim Peters2a799bf2002-12-16 20:18:38 +00002945};
2946
2947/*
Tim Peters2a799bf2002-12-16 20:18:38 +00002948 * PyDateTime_TZInfo implementation.
2949 */
2950
2951/* This is a pure abstract base class, so doesn't do anything beyond
2952 * raising NotImplemented exceptions. Real tzinfo classes need
2953 * to derive from this. This is mostly for clarity, and for efficiency in
Tim Petersa9bc1682003-01-11 03:39:11 +00002954 * datetime and time constructors (their tzinfo arguments need to
Tim Peters2a799bf2002-12-16 20:18:38 +00002955 * be subclasses of this tzinfo class, which is easy and quick to check).
2956 *
2957 * Note: For reasons having to do with pickling of subclasses, we have
2958 * to allow tzinfo objects to be instantiated. This wasn't an issue
2959 * in the Python implementation (__init__() could raise NotImplementedError
2960 * there without ill effect), but doing so in the C implementation hit a
2961 * brick wall.
2962 */
2963
2964static PyObject *
2965tzinfo_nogo(const char* methodname)
2966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 PyErr_Format(PyExc_NotImplementedError,
2968 "a tzinfo subclass must implement %s()",
2969 methodname);
2970 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00002971}
2972
2973/* Methods. A subclass must implement these. */
2974
Tim Peters52dcce22003-01-23 16:36:11 +00002975static PyObject *
Tim Peters2a799bf2002-12-16 20:18:38 +00002976tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt)
2977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 return tzinfo_nogo("tzname");
Tim Peters2a799bf2002-12-16 20:18:38 +00002979}
2980
Tim Peters52dcce22003-01-23 16:36:11 +00002981static PyObject *
Tim Peters2a799bf2002-12-16 20:18:38 +00002982tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt)
2983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 return tzinfo_nogo("utcoffset");
Tim Peters2a799bf2002-12-16 20:18:38 +00002985}
2986
Tim Peters52dcce22003-01-23 16:36:11 +00002987static PyObject *
Tim Peters2a799bf2002-12-16 20:18:38 +00002988tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt)
2989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 return tzinfo_nogo("dst");
Tim Peters2a799bf2002-12-16 20:18:38 +00002991}
2992
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00002993
2994static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date,
2995 PyDateTime_Delta *delta,
2996 int factor);
2997static PyObject *datetime_utcoffset(PyObject *self, PyObject *);
2998static PyObject *datetime_dst(PyObject *self, PyObject *);
2999
Tim Peters52dcce22003-01-23 16:36:11 +00003000static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003001tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt)
Tim Peters52dcce22003-01-23 16:36:11 +00003002{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003003 PyObject *result = NULL;
3004 PyObject *off = NULL, *dst = NULL;
3005 PyDateTime_Delta *delta = NULL;
Tim Peters52dcce22003-01-23 16:36:11 +00003006
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003007 if (!PyDateTime_Check(dt)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 PyErr_SetString(PyExc_TypeError,
3009 "fromutc: argument must be a datetime");
3010 return NULL;
3011 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003012 if (GET_DT_TZINFO(dt) != (PyObject *)self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo "
3014 "is not self");
3015 return NULL;
3016 }
Tim Peters52dcce22003-01-23 16:36:11 +00003017
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003018 off = datetime_utcoffset(dt, NULL);
3019 if (off == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 return NULL;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003021 if (off == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
3023 "utcoffset() result required");
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003024 goto Fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 }
Tim Peters52dcce22003-01-23 16:36:11 +00003026
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003027 dst = datetime_dst(dt, NULL);
3028 if (dst == NULL)
3029 goto Fail;
3030 if (dst == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
3032 "dst() result required");
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003033 goto Fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 }
Tim Peters52dcce22003-01-23 16:36:11 +00003035
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003036 delta = (PyDateTime_Delta *)delta_subtract(off, dst);
3037 if (delta == NULL)
3038 goto Fail;
3039 result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 if (result == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 goto Fail;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003042
3043 Py_DECREF(dst);
3044 dst = call_dst(GET_DT_TZINFO(dt), result);
3045 if (dst == NULL)
3046 goto Fail;
3047 if (dst == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 goto Inconsistent;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003049 if (delta_bool(delta) != 0) {
3050 PyObject *temp = result;
3051 result = add_datetime_timedelta((PyDateTime_DateTime *)result,
3052 (PyDateTime_Delta *)dst, 1);
3053 Py_DECREF(temp);
3054 if (result == NULL)
3055 goto Fail;
3056 }
3057 Py_DECREF(delta);
3058 Py_DECREF(dst);
3059 Py_DECREF(off);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 return result;
Tim Peters52dcce22003-01-23 16:36:11 +00003061
3062Inconsistent:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave"
3064 "inconsistent results; cannot convert");
Tim Peters52dcce22003-01-23 16:36:11 +00003065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 /* fall thru to failure */
Tim Peters52dcce22003-01-23 16:36:11 +00003067Fail:
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003068 Py_XDECREF(off);
3069 Py_XDECREF(dst);
3070 Py_XDECREF(delta);
3071 Py_XDECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 return NULL;
Tim Peters52dcce22003-01-23 16:36:11 +00003073}
3074
Tim Peters2a799bf2002-12-16 20:18:38 +00003075/*
3076 * Pickle support. This is solely so that tzinfo subclasses can use
Guido van Rossum177e41a2003-01-30 22:06:23 +00003077 * pickling -- tzinfo itself is supposed to be uninstantiable.
Tim Peters2a799bf2002-12-16 20:18:38 +00003078 */
3079
Guido van Rossum177e41a2003-01-30 22:06:23 +00003080static PyObject *
3081tzinfo_reduce(PyObject *self)
3082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 PyObject *args, *state, *tmp;
3084 PyObject *getinitargs, *getstate;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003085 _Py_IDENTIFIER(__getinitargs__);
3086 _Py_IDENTIFIER(__getstate__);
Tim Peters2a799bf2002-12-16 20:18:38 +00003087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 tmp = PyTuple_New(0);
3089 if (tmp == NULL)
3090 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00003091
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003092 getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 if (getinitargs != NULL) {
3094 args = PyObject_CallObject(getinitargs, tmp);
3095 Py_DECREF(getinitargs);
3096 if (args == NULL) {
3097 Py_DECREF(tmp);
3098 return NULL;
3099 }
3100 }
3101 else {
3102 PyErr_Clear();
3103 args = tmp;
3104 Py_INCREF(args);
3105 }
Guido van Rossum177e41a2003-01-30 22:06:23 +00003106
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003107 getstate = _PyObject_GetAttrId(self, &PyId___getstate__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 if (getstate != NULL) {
3109 state = PyObject_CallObject(getstate, tmp);
3110 Py_DECREF(getstate);
3111 if (state == NULL) {
3112 Py_DECREF(args);
3113 Py_DECREF(tmp);
3114 return NULL;
3115 }
3116 }
3117 else {
3118 PyObject **dictptr;
3119 PyErr_Clear();
3120 state = Py_None;
3121 dictptr = _PyObject_GetDictPtr(self);
3122 if (dictptr && *dictptr && PyDict_Size(*dictptr))
3123 state = *dictptr;
3124 Py_INCREF(state);
3125 }
Guido van Rossum177e41a2003-01-30 22:06:23 +00003126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 Py_DECREF(tmp);
Guido van Rossum177e41a2003-01-30 22:06:23 +00003128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 if (state == Py_None) {
3130 Py_DECREF(state);
3131 return Py_BuildValue("(ON)", Py_TYPE(self), args);
3132 }
3133 else
3134 return Py_BuildValue("(ONN)", Py_TYPE(self), args, state);
Guido van Rossum177e41a2003-01-30 22:06:23 +00003135}
Tim Peters2a799bf2002-12-16 20:18:38 +00003136
3137static PyMethodDef tzinfo_methods[] = {
Guido van Rossum177e41a2003-01-30 22:06:23 +00003138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 {"tzname", (PyCFunction)tzinfo_tzname, METH_O,
3140 PyDoc_STR("datetime -> string name of time zone.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O,
Sean Reifscheiderdeda8cb2010-06-04 01:51:38 +00003143 PyDoc_STR("datetime -> timedelta showing offset from UTC, negative "
3144 "values indicating West of UTC")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 {"dst", (PyCFunction)tzinfo_dst, METH_O,
3147 PyDoc_STR("datetime -> DST offset in minutes east of UTC.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O,
Alexander Belopolsky2f194b92010-07-03 03:35:27 +00003150 PyDoc_STR("datetime in UTC -> datetime in local time.")},
Tim Peters52dcce22003-01-23 16:36:11 +00003151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS,
3153 PyDoc_STR("-> (cls, state)")},
Guido van Rossum177e41a2003-01-30 22:06:23 +00003154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 {NULL, NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00003156};
3157
3158static char tzinfo_doc[] =
3159PyDoc_STR("Abstract base class for time zone info objects.");
3160
Neal Norwitz227b5332006-03-22 09:28:35 +00003161static PyTypeObject PyDateTime_TZInfoType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 PyVarObject_HEAD_INIT(NULL, 0)
3163 "datetime.tzinfo", /* tp_name */
3164 sizeof(PyDateTime_TZInfo), /* tp_basicsize */
3165 0, /* tp_itemsize */
3166 0, /* tp_dealloc */
3167 0, /* tp_print */
3168 0, /* tp_getattr */
3169 0, /* tp_setattr */
3170 0, /* tp_reserved */
3171 0, /* tp_repr */
3172 0, /* tp_as_number */
3173 0, /* tp_as_sequence */
3174 0, /* tp_as_mapping */
3175 0, /* tp_hash */
3176 0, /* tp_call */
3177 0, /* tp_str */
3178 PyObject_GenericGetAttr, /* tp_getattro */
3179 0, /* tp_setattro */
3180 0, /* tp_as_buffer */
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003181 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 tzinfo_doc, /* tp_doc */
3183 0, /* tp_traverse */
3184 0, /* tp_clear */
3185 0, /* tp_richcompare */
3186 0, /* tp_weaklistoffset */
3187 0, /* tp_iter */
3188 0, /* tp_iternext */
3189 tzinfo_methods, /* tp_methods */
3190 0, /* tp_members */
3191 0, /* tp_getset */
3192 0, /* tp_base */
3193 0, /* tp_dict */
3194 0, /* tp_descr_get */
3195 0, /* tp_descr_set */
3196 0, /* tp_dictoffset */
3197 0, /* tp_init */
3198 0, /* tp_alloc */
3199 PyType_GenericNew, /* tp_new */
3200 0, /* tp_free */
Tim Peters2a799bf2002-12-16 20:18:38 +00003201};
3202
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003203static char *timezone_kws[] = {"offset", "name", NULL};
3204
3205static PyObject *
3206timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw)
3207{
3208 PyObject *offset;
3209 PyObject *name = NULL;
3210 if (PyArg_ParseTupleAndKeywords(args, kw, "O!|O!:timezone", timezone_kws,
3211 &PyDateTime_DeltaType, &offset,
3212 &PyUnicode_Type, &name))
3213 return new_timezone(offset, name);
3214
3215 return NULL;
3216}
3217
3218static void
3219timezone_dealloc(PyDateTime_TimeZone *self)
3220{
3221 Py_CLEAR(self->offset);
3222 Py_CLEAR(self->name);
3223 Py_TYPE(self)->tp_free((PyObject *)self);
3224}
3225
3226static PyObject *
3227timezone_richcompare(PyDateTime_TimeZone *self,
3228 PyDateTime_TimeZone *other, int op)
3229{
Brian Curtindfc80e32011-08-10 20:28:54 -05003230 if (op != Py_EQ && op != Py_NE)
3231 Py_RETURN_NOTIMPLEMENTED;
Georg Brandl0085a242012-09-22 09:23:12 +02003232 if (Py_TYPE(other) != &PyDateTime_TimeZoneType) {
Raymond Hettinger5a2146a2014-07-25 14:59:48 -07003233 if (op == Py_EQ)
3234 Py_RETURN_FALSE;
3235 else
3236 Py_RETURN_TRUE;
Georg Brandl0085a242012-09-22 09:23:12 +02003237 }
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003238 return delta_richcompare(self->offset, other->offset, op);
3239}
3240
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003241static Py_hash_t
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003242timezone_hash(PyDateTime_TimeZone *self)
3243{
3244 return delta_hash((PyDateTime_Delta *)self->offset);
3245}
3246
3247/* Check argument type passed to tzname, utcoffset, or dst methods.
3248 Returns 0 for good argument. Returns -1 and sets exception info
3249 otherwise.
3250 */
3251static int
3252_timezone_check_argument(PyObject *dt, const char *meth)
3253{
3254 if (dt == Py_None || PyDateTime_Check(dt))
3255 return 0;
3256 PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance"
3257 " or None, not %.200s", meth, Py_TYPE(dt)->tp_name);
3258 return -1;
3259}
3260
3261static PyObject *
Alexander Belopolskya11d8c02010-07-06 23:19:45 +00003262timezone_repr(PyDateTime_TimeZone *self)
3263{
3264 /* Note that although timezone is not subclassable, it is convenient
3265 to use Py_TYPE(self)->tp_name here. */
3266 const char *type_name = Py_TYPE(self)->tp_name;
3267
3268 if (((PyObject *)self) == PyDateTime_TimeZone_UTC)
3269 return PyUnicode_FromFormat("%s.utc", type_name);
3270
3271 if (self->name == NULL)
3272 return PyUnicode_FromFormat("%s(%R)", type_name, self->offset);
3273
3274 return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset,
3275 self->name);
3276}
3277
3278
3279static PyObject *
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003280timezone_str(PyDateTime_TimeZone *self)
3281{
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003282 int hours, minutes, seconds;
3283 PyObject *offset;
3284 char sign;
3285
3286 if (self->name != NULL) {
3287 Py_INCREF(self->name);
3288 return self->name;
3289 }
3290 /* Offset is normalized, so it is negative if days < 0 */
3291 if (GET_TD_DAYS(self->offset) < 0) {
3292 sign = '-';
3293 offset = delta_negative((PyDateTime_Delta *)self->offset);
3294 if (offset == NULL)
3295 return NULL;
3296 }
3297 else {
3298 sign = '+';
3299 offset = self->offset;
3300 Py_INCREF(offset);
3301 }
3302 /* Offset is not negative here. */
3303 seconds = GET_TD_SECONDS(offset);
3304 Py_DECREF(offset);
3305 minutes = divmod(seconds, 60, &seconds);
3306 hours = divmod(minutes, 60, &minutes);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003307 /* XXX ignore sub-minute data, curently not allowed. */
Victor Stinner6ced7c42011-03-21 18:15:42 +01003308 assert(seconds == 0);
3309 return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003310}
3311
3312static PyObject *
3313timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt)
3314{
3315 if (_timezone_check_argument(dt, "tzname") == -1)
3316 return NULL;
3317
3318 return timezone_str(self);
3319}
3320
3321static PyObject *
3322timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt)
3323{
3324 if (_timezone_check_argument(dt, "utcoffset") == -1)
3325 return NULL;
3326
3327 Py_INCREF(self->offset);
3328 return self->offset;
3329}
3330
3331static PyObject *
3332timezone_dst(PyObject *self, PyObject *dt)
3333{
3334 if (_timezone_check_argument(dt, "dst") == -1)
3335 return NULL;
3336
3337 Py_RETURN_NONE;
3338}
3339
3340static PyObject *
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003341timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt)
3342{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003343 if (!PyDateTime_Check(dt)) {
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003344 PyErr_SetString(PyExc_TypeError,
3345 "fromutc: argument must be a datetime");
3346 return NULL;
3347 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003348 if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) {
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003349 PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo "
3350 "is not self");
3351 return NULL;
3352 }
3353
3354 return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1);
3355}
3356
Alexander Belopolsky1b7046b2010-06-23 21:40:15 +00003357static PyObject *
3358timezone_getinitargs(PyDateTime_TimeZone *self)
3359{
3360 if (self->name == NULL)
3361 return Py_BuildValue("(O)", self->offset);
3362 return Py_BuildValue("(OO)", self->offset, self->name);
3363}
3364
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003365static PyMethodDef timezone_methods[] = {
3366 {"tzname", (PyCFunction)timezone_tzname, METH_O,
3367 PyDoc_STR("If name is specified when timezone is created, returns the name."
Alexander Belopolskyb39a0c22010-06-15 19:24:52 +00003368 " Otherwise returns offset as 'UTC(+|-)HH:MM'.")},
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003369
3370 {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O,
Alexander Belopolskyb39a0c22010-06-15 19:24:52 +00003371 PyDoc_STR("Return fixed offset.")},
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003372
3373 {"dst", (PyCFunction)timezone_dst, METH_O,
Alexander Belopolskyb39a0c22010-06-15 19:24:52 +00003374 PyDoc_STR("Return None.")},
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003375
3376 {"fromutc", (PyCFunction)timezone_fromutc, METH_O,
3377 PyDoc_STR("datetime in UTC -> datetime in local time.")},
3378
Alexander Belopolsky1b7046b2010-06-23 21:40:15 +00003379 {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS,
3380 PyDoc_STR("pickle support")},
3381
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003382 {NULL, NULL}
3383};
3384
3385static char timezone_doc[] =
3386PyDoc_STR("Fixed offset from UTC implementation of tzinfo.");
3387
3388static PyTypeObject PyDateTime_TimeZoneType = {
3389 PyVarObject_HEAD_INIT(NULL, 0)
3390 "datetime.timezone", /* tp_name */
3391 sizeof(PyDateTime_TimeZone), /* tp_basicsize */
3392 0, /* tp_itemsize */
3393 (destructor)timezone_dealloc, /* tp_dealloc */
3394 0, /* tp_print */
3395 0, /* tp_getattr */
3396 0, /* tp_setattr */
3397 0, /* tp_reserved */
Alexander Belopolskya11d8c02010-07-06 23:19:45 +00003398 (reprfunc)timezone_repr, /* tp_repr */
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00003399 0, /* tp_as_number */
3400 0, /* tp_as_sequence */
3401 0, /* tp_as_mapping */
3402 (hashfunc)timezone_hash, /* tp_hash */
3403 0, /* tp_call */
3404 (reprfunc)timezone_str, /* tp_str */
3405 0, /* tp_getattro */
3406 0, /* tp_setattro */
3407 0, /* tp_as_buffer */
3408 Py_TPFLAGS_DEFAULT, /* tp_flags */
3409 timezone_doc, /* tp_doc */
3410 0, /* tp_traverse */
3411 0, /* tp_clear */
3412 (richcmpfunc)timezone_richcompare,/* tp_richcompare */
3413 0, /* tp_weaklistoffset */
3414 0, /* tp_iter */
3415 0, /* tp_iternext */
3416 timezone_methods, /* tp_methods */
3417 0, /* tp_members */
3418 0, /* tp_getset */
3419 &PyDateTime_TZInfoType, /* tp_base */
3420 0, /* tp_dict */
3421 0, /* tp_descr_get */
3422 0, /* tp_descr_set */
3423 0, /* tp_dictoffset */
3424 0, /* tp_init */
3425 0, /* tp_alloc */
3426 timezone_new, /* tp_new */
3427};
3428
Tim Peters2a799bf2002-12-16 20:18:38 +00003429/*
Tim Peters37f39822003-01-10 03:49:02 +00003430 * PyDateTime_Time implementation.
Tim Peters2a799bf2002-12-16 20:18:38 +00003431 */
3432
Tim Peters37f39822003-01-10 03:49:02 +00003433/* Accessor properties.
Tim Peters2a799bf2002-12-16 20:18:38 +00003434 */
3435
3436static PyObject *
Tim Peters37f39822003-01-10 03:49:02 +00003437time_hour(PyDateTime_Time *self, void *unused)
Tim Peters2a799bf2002-12-16 20:18:38 +00003438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 return PyLong_FromLong(TIME_GET_HOUR(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00003440}
3441
Tim Peters37f39822003-01-10 03:49:02 +00003442static PyObject *
3443time_minute(PyDateTime_Time *self, void *unused)
3444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 return PyLong_FromLong(TIME_GET_MINUTE(self));
Tim Peters37f39822003-01-10 03:49:02 +00003446}
3447
3448/* The name time_second conflicted with some platform header file. */
3449static PyObject *
3450py_time_second(PyDateTime_Time *self, void *unused)
3451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 return PyLong_FromLong(TIME_GET_SECOND(self));
Tim Peters37f39822003-01-10 03:49:02 +00003453}
3454
3455static PyObject *
3456time_microsecond(PyDateTime_Time *self, void *unused)
3457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 return PyLong_FromLong(TIME_GET_MICROSECOND(self));
Tim Peters37f39822003-01-10 03:49:02 +00003459}
3460
3461static PyObject *
3462time_tzinfo(PyDateTime_Time *self, void *unused)
3463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
3465 Py_INCREF(result);
3466 return result;
Tim Peters37f39822003-01-10 03:49:02 +00003467}
3468
3469static PyGetSetDef time_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 {"hour", (getter)time_hour},
3471 {"minute", (getter)time_minute},
3472 {"second", (getter)py_time_second},
3473 {"microsecond", (getter)time_microsecond},
3474 {"tzinfo", (getter)time_tzinfo},
3475 {NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00003476};
3477
3478/*
3479 * Constructors.
3480 */
3481
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +00003482static char *time_kws[] = {"hour", "minute", "second", "microsecond",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 "tzinfo", NULL};
Tim Peters12bf3392002-12-24 05:41:27 +00003484
Tim Peters2a799bf2002-12-16 20:18:38 +00003485static PyObject *
Tim Peters37f39822003-01-10 03:49:02 +00003486time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
Tim Peters2a799bf2002-12-16 20:18:38 +00003487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 PyObject *self = NULL;
3489 PyObject *state;
3490 int hour = 0;
3491 int minute = 0;
3492 int second = 0;
3493 int usecond = 0;
3494 PyObject *tzinfo = Py_None;
Tim Peters2a799bf2002-12-16 20:18:38 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 /* Check for invocation from pickle with __getstate__ state */
3497 if (PyTuple_GET_SIZE(args) >= 1 &&
3498 PyTuple_GET_SIZE(args) <= 2 &&
3499 PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
3500 PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
3501 ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24)
3502 {
3503 PyDateTime_Time *me;
3504 char aware;
Tim Peters70533e22003-02-01 04:40:04 +00003505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 if (PyTuple_GET_SIZE(args) == 2) {
3507 tzinfo = PyTuple_GET_ITEM(args, 1);
3508 if (check_tzinfo_subclass(tzinfo) < 0) {
3509 PyErr_SetString(PyExc_TypeError, "bad "
3510 "tzinfo state arg");
3511 return NULL;
3512 }
3513 }
3514 aware = (char)(tzinfo != Py_None);
3515 me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
3516 if (me != NULL) {
3517 char *pdata = PyBytes_AS_STRING(state);
Tim Peters70533e22003-02-01 04:40:04 +00003518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
3520 me->hashcode = -1;
3521 me->hastzinfo = aware;
3522 if (aware) {
3523 Py_INCREF(tzinfo);
3524 me->tzinfo = tzinfo;
3525 }
3526 }
3527 return (PyObject *)me;
3528 }
Guido van Rossum177e41a2003-01-30 22:06:23 +00003529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws,
3531 &hour, &minute, &second, &usecond,
3532 &tzinfo)) {
3533 if (check_time_args(hour, minute, second, usecond) < 0)
3534 return NULL;
3535 if (check_tzinfo_subclass(tzinfo) < 0)
3536 return NULL;
3537 self = new_time_ex(hour, minute, second, usecond, tzinfo,
3538 type);
3539 }
3540 return self;
Tim Peters2a799bf2002-12-16 20:18:38 +00003541}
3542
3543/*
3544 * Destructor.
3545 */
3546
3547static void
Tim Peters37f39822003-01-10 03:49:02 +00003548time_dealloc(PyDateTime_Time *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00003549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 if (HASTZINFO(self)) {
3551 Py_XDECREF(self->tzinfo);
3552 }
3553 Py_TYPE(self)->tp_free((PyObject *)self);
Tim Peters2a799bf2002-12-16 20:18:38 +00003554}
3555
3556/*
Tim Peters855fe882002-12-22 03:43:39 +00003557 * Indirect access to tzinfo methods.
Tim Peters2a799bf2002-12-16 20:18:38 +00003558 */
3559
Tim Peters2a799bf2002-12-16 20:18:38 +00003560/* These are all METH_NOARGS, so don't need to check the arglist. */
3561static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003562time_utcoffset(PyObject *self, PyObject *unused) {
3563 return call_utcoffset(GET_TIME_TZINFO(self), Py_None);
Tim Peters2a799bf2002-12-16 20:18:38 +00003564}
3565
3566static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003567time_dst(PyObject *self, PyObject *unused) {
3568 return call_dst(GET_TIME_TZINFO(self), Py_None);
Tim Peters855fe882002-12-22 03:43:39 +00003569}
3570
3571static PyObject *
Tim Peters37f39822003-01-10 03:49:02 +00003572time_tzname(PyDateTime_Time *self, PyObject *unused) {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003573 return call_tzname(GET_TIME_TZINFO(self), Py_None);
Tim Peters2a799bf2002-12-16 20:18:38 +00003574}
3575
3576/*
Tim Peters37f39822003-01-10 03:49:02 +00003577 * Various ways to turn a time into a string.
Tim Peters2a799bf2002-12-16 20:18:38 +00003578 */
3579
3580static PyObject *
Tim Peters37f39822003-01-10 03:49:02 +00003581time_repr(PyDateTime_Time *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00003582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 const char *type_name = Py_TYPE(self)->tp_name;
3584 int h = TIME_GET_HOUR(self);
3585 int m = TIME_GET_MINUTE(self);
3586 int s = TIME_GET_SECOND(self);
3587 int us = TIME_GET_MICROSECOND(self);
3588 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 if (us)
3591 result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)",
3592 type_name, h, m, s, us);
3593 else if (s)
3594 result = PyUnicode_FromFormat("%s(%d, %d, %d)",
3595 type_name, h, m, s);
3596 else
3597 result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m);
3598 if (result != NULL && HASTZINFO(self))
3599 result = append_keyword_tzinfo(result, self->tzinfo);
3600 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00003601}
3602
Tim Peters37f39822003-01-10 03:49:02 +00003603static PyObject *
3604time_str(PyDateTime_Time *self)
3605{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003606 return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()");
Tim Peters37f39822003-01-10 03:49:02 +00003607}
Tim Peters2a799bf2002-12-16 20:18:38 +00003608
3609static PyObject *
Thomas Wouterscf297e42007-02-23 15:07:44 +00003610time_isoformat(PyDateTime_Time *self, PyObject *unused)
Tim Peters2a799bf2002-12-16 20:18:38 +00003611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 char buf[100];
3613 PyObject *result;
Ezio Melotti3f5db392013-01-27 06:20:14 +02003614 int us = TIME_GET_MICROSECOND(self);
Tim Peters2a799bf2002-12-16 20:18:38 +00003615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 if (us)
3617 result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d",
3618 TIME_GET_HOUR(self),
3619 TIME_GET_MINUTE(self),
3620 TIME_GET_SECOND(self),
3621 us);
3622 else
3623 result = PyUnicode_FromFormat("%02d:%02d:%02d",
3624 TIME_GET_HOUR(self),
3625 TIME_GET_MINUTE(self),
3626 TIME_GET_SECOND(self));
Tim Peters37f39822003-01-10 03:49:02 +00003627
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003628 if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 /* We need to append the UTC offset. */
3632 if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo,
3633 Py_None) < 0) {
3634 Py_DECREF(result);
3635 return NULL;
3636 }
3637 PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf));
3638 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00003639}
3640
Tim Peters37f39822003-01-10 03:49:02 +00003641static PyObject *
3642time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw)
3643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 PyObject *result;
3645 PyObject *tuple;
3646 PyObject *format;
3647 static char *keywords[] = {"format", NULL};
Tim Peters37f39822003-01-10 03:49:02 +00003648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
3650 &format))
3651 return NULL;
Tim Peters37f39822003-01-10 03:49:02 +00003652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 /* Python's strftime does insane things with the year part of the
3654 * timetuple. The year is forced to (the otherwise nonsensical)
Alexander Belopolskyb8bb4662011-01-08 00:13:34 +00003655 * 1900 to work around that.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 */
3657 tuple = Py_BuildValue("iiiiiiiii",
3658 1900, 1, 1, /* year, month, day */
3659 TIME_GET_HOUR(self),
3660 TIME_GET_MINUTE(self),
3661 TIME_GET_SECOND(self),
3662 0, 1, -1); /* weekday, daynum, dst */
3663 if (tuple == NULL)
3664 return NULL;
3665 assert(PyTuple_Size(tuple) == 9);
3666 result = wrap_strftime((PyObject *)self, format, tuple,
3667 Py_None);
3668 Py_DECREF(tuple);
3669 return result;
Tim Peters37f39822003-01-10 03:49:02 +00003670}
Tim Peters2a799bf2002-12-16 20:18:38 +00003671
3672/*
3673 * Miscellaneous methods.
3674 */
3675
Tim Peters37f39822003-01-10 03:49:02 +00003676static PyObject *
Guido van Rossum19960592006-08-24 17:29:38 +00003677time_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters37f39822003-01-10 03:49:02 +00003678{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003679 PyObject *result = NULL;
3680 PyObject *offset1, *offset2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 int diff;
Tim Peters37f39822003-01-10 03:49:02 +00003682
Brian Curtindfc80e32011-08-10 20:28:54 -05003683 if (! PyTime_Check(other))
3684 Py_RETURN_NOTIMPLEMENTED;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003685
3686 if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 diff = memcmp(((PyDateTime_Time *)self)->data,
3688 ((PyDateTime_Time *)other)->data,
3689 _PyDateTime_TIME_DATASIZE);
3690 return diff_to_bool(diff, op);
3691 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003692 offset1 = time_utcoffset(self, NULL);
3693 if (offset1 == NULL)
3694 return NULL;
3695 offset2 = time_utcoffset(other, NULL);
3696 if (offset2 == NULL)
3697 goto done;
3698 /* If they're both naive, or both aware and have the same offsets,
3699 * we get off cheap. Note that if they're both naive, offset1 ==
3700 * offset2 == Py_None at this point.
3701 */
3702 if ((offset1 == offset2) ||
3703 (PyDelta_Check(offset1) && PyDelta_Check(offset2) &&
3704 delta_cmp(offset1, offset2) == 0)) {
3705 diff = memcmp(((PyDateTime_Time *)self)->data,
3706 ((PyDateTime_Time *)other)->data,
3707 _PyDateTime_TIME_DATASIZE);
3708 result = diff_to_bool(diff, op);
3709 }
3710 /* The hard case: both aware with different UTC offsets */
3711 else if (offset1 != Py_None && offset2 != Py_None) {
3712 int offsecs1, offsecs2;
3713 assert(offset1 != offset2); /* else last "if" handled it */
3714 offsecs1 = TIME_GET_HOUR(self) * 3600 +
3715 TIME_GET_MINUTE(self) * 60 +
3716 TIME_GET_SECOND(self) -
3717 GET_TD_DAYS(offset1) * 86400 -
3718 GET_TD_SECONDS(offset1);
3719 offsecs2 = TIME_GET_HOUR(other) * 3600 +
3720 TIME_GET_MINUTE(other) * 60 +
3721 TIME_GET_SECOND(other) -
3722 GET_TD_DAYS(offset2) * 86400 -
3723 GET_TD_SECONDS(offset2);
3724 diff = offsecs1 - offsecs2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 if (diff == 0)
3726 diff = TIME_GET_MICROSECOND(self) -
3727 TIME_GET_MICROSECOND(other);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003728 result = diff_to_bool(diff, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 }
Alexander Belopolsky08313822012-06-15 20:19:47 -04003730 else if (op == Py_EQ) {
3731 result = Py_False;
3732 Py_INCREF(result);
3733 }
3734 else if (op == Py_NE) {
3735 result = Py_True;
3736 Py_INCREF(result);
3737 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003738 else {
3739 PyErr_SetString(PyExc_TypeError,
3740 "can't compare offset-naive and "
3741 "offset-aware times");
3742 }
3743 done:
3744 Py_DECREF(offset1);
3745 Py_XDECREF(offset2);
3746 return result;
Tim Peters37f39822003-01-10 03:49:02 +00003747}
3748
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003749static Py_hash_t
Tim Peters37f39822003-01-10 03:49:02 +00003750time_hash(PyDateTime_Time *self)
3751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 if (self->hashcode == -1) {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003753 PyObject *offset;
Tim Peters37f39822003-01-10 03:49:02 +00003754
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003755 offset = time_utcoffset((PyObject *)self, NULL);
3756
3757 if (offset == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 return -1;
Tim Peters37f39822003-01-10 03:49:02 +00003759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 /* Reduce this to a hash of another object. */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003761 if (offset == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 self->hashcode = generic_hash(
3763 (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 else {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003765 PyObject *temp1, *temp2;
3766 int seconds, microseconds;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 assert(HASTZINFO(self));
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003768 seconds = TIME_GET_HOUR(self) * 3600 +
3769 TIME_GET_MINUTE(self) * 60 +
3770 TIME_GET_SECOND(self);
3771 microseconds = TIME_GET_MICROSECOND(self);
3772 temp1 = new_delta(0, seconds, microseconds, 1);
3773 if (temp1 == NULL) {
3774 Py_DECREF(offset);
3775 return -1;
3776 }
3777 temp2 = delta_subtract(temp1, offset);
3778 Py_DECREF(temp1);
3779 if (temp2 == NULL) {
3780 Py_DECREF(offset);
3781 return -1;
3782 }
3783 self->hashcode = PyObject_Hash(temp2);
3784 Py_DECREF(temp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00003786 Py_DECREF(offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 }
3788 return self->hashcode;
Tim Peters37f39822003-01-10 03:49:02 +00003789}
Tim Peters2a799bf2002-12-16 20:18:38 +00003790
Tim Peters12bf3392002-12-24 05:41:27 +00003791static PyObject *
Tim Peters37f39822003-01-10 03:49:02 +00003792time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
Tim Peters12bf3392002-12-24 05:41:27 +00003793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 PyObject *clone;
3795 PyObject *tuple;
3796 int hh = TIME_GET_HOUR(self);
3797 int mm = TIME_GET_MINUTE(self);
3798 int ss = TIME_GET_SECOND(self);
3799 int us = TIME_GET_MICROSECOND(self);
3800 PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
Tim Peters12bf3392002-12-24 05:41:27 +00003801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace",
3803 time_kws,
3804 &hh, &mm, &ss, &us, &tzinfo))
3805 return NULL;
3806 tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
3807 if (tuple == NULL)
3808 return NULL;
3809 clone = time_new(Py_TYPE(self), tuple, NULL);
3810 Py_DECREF(tuple);
3811 return clone;
Tim Peters12bf3392002-12-24 05:41:27 +00003812}
3813
Tim Peters371935f2003-02-01 01:52:50 +00003814/* Pickle support, a simple use of __reduce__. */
Tim Peters2a799bf2002-12-16 20:18:38 +00003815
Tim Peters33e0f382003-01-10 02:05:14 +00003816/* Let basestate be the non-tzinfo data string.
Tim Peters2a799bf2002-12-16 20:18:38 +00003817 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
3818 * So it's a tuple in any (non-error) case.
Tim Petersb57f8f02003-02-01 02:54:15 +00003819 * __getstate__ isn't exposed.
Tim Peters2a799bf2002-12-16 20:18:38 +00003820 */
3821static PyObject *
Tim Peters37f39822003-01-10 03:49:02 +00003822time_getstate(PyDateTime_Time *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00003823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 PyObject *basestate;
3825 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00003826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 basestate = PyBytes_FromStringAndSize((char *)self->data,
3828 _PyDateTime_TIME_DATASIZE);
3829 if (basestate != NULL) {
3830 if (! HASTZINFO(self) || self->tzinfo == Py_None)
3831 result = PyTuple_Pack(1, basestate);
3832 else
3833 result = PyTuple_Pack(2, basestate, self->tzinfo);
3834 Py_DECREF(basestate);
3835 }
3836 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00003837}
3838
3839static PyObject *
Guido van Rossum177e41a2003-01-30 22:06:23 +00003840time_reduce(PyDateTime_Time *self, PyObject *arg)
Tim Peters2a799bf2002-12-16 20:18:38 +00003841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00003843}
3844
Tim Peters37f39822003-01-10 03:49:02 +00003845static PyMethodDef time_methods[] = {
Guido van Rossum177e41a2003-01-30 22:06:23 +00003846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS,
3848 PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]"
3849 "[+HH:MM].")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS,
3852 PyDoc_STR("format -> strftime() style string.")},
Tim Peters37f39822003-01-10 03:49:02 +00003853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 {"__format__", (PyCFunction)date_format, METH_VARARGS,
3855 PyDoc_STR("Formats self with strftime.")},
Eric Smith1ba31142007-09-11 18:06:02 +00003856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS,
3858 PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 {"tzname", (PyCFunction)time_tzname, METH_NOARGS,
3861 PyDoc_STR("Return self.tzinfo.tzname(self).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 {"dst", (PyCFunction)time_dst, METH_NOARGS,
3864 PyDoc_STR("Return self.tzinfo.dst(self).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00003865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS,
3867 PyDoc_STR("Return time with new specified fields.")},
Tim Peters12bf3392002-12-24 05:41:27 +00003868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
3870 PyDoc_STR("__reduce__() -> (cls, state)")},
Guido van Rossum177e41a2003-01-30 22:06:23 +00003871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 {NULL, NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00003873};
3874
Tim Peters37f39822003-01-10 03:49:02 +00003875static char time_doc[] =
Raymond Hettinger3a4231d2004-12-19 20:13:24 +00003876PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\
3877\n\
3878All arguments are optional. tzinfo may be None, or an instance of\n\
Serhiy Storchaka95949422013-08-27 19:40:23 +03003879a tzinfo subclass. The remaining arguments may be ints.\n");
Tim Peters2a799bf2002-12-16 20:18:38 +00003880
Neal Norwitz227b5332006-03-22 09:28:35 +00003881static PyTypeObject PyDateTime_TimeType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 PyVarObject_HEAD_INIT(NULL, 0)
3883 "datetime.time", /* tp_name */
3884 sizeof(PyDateTime_Time), /* tp_basicsize */
3885 0, /* tp_itemsize */
3886 (destructor)time_dealloc, /* tp_dealloc */
3887 0, /* tp_print */
3888 0, /* tp_getattr */
3889 0, /* tp_setattr */
3890 0, /* tp_reserved */
3891 (reprfunc)time_repr, /* tp_repr */
Benjamin Petersonee6bdc02014-03-20 18:00:35 -05003892 0, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 0, /* tp_as_sequence */
3894 0, /* tp_as_mapping */
3895 (hashfunc)time_hash, /* tp_hash */
3896 0, /* tp_call */
3897 (reprfunc)time_str, /* tp_str */
3898 PyObject_GenericGetAttr, /* tp_getattro */
3899 0, /* tp_setattro */
3900 0, /* tp_as_buffer */
3901 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3902 time_doc, /* tp_doc */
3903 0, /* tp_traverse */
3904 0, /* tp_clear */
3905 time_richcompare, /* tp_richcompare */
3906 0, /* tp_weaklistoffset */
3907 0, /* tp_iter */
3908 0, /* tp_iternext */
3909 time_methods, /* tp_methods */
3910 0, /* tp_members */
3911 time_getset, /* tp_getset */
3912 0, /* tp_base */
3913 0, /* tp_dict */
3914 0, /* tp_descr_get */
3915 0, /* tp_descr_set */
3916 0, /* tp_dictoffset */
3917 0, /* tp_init */
3918 time_alloc, /* tp_alloc */
3919 time_new, /* tp_new */
3920 0, /* tp_free */
Tim Peters2a799bf2002-12-16 20:18:38 +00003921};
3922
3923/*
Tim Petersa9bc1682003-01-11 03:39:11 +00003924 * PyDateTime_DateTime implementation.
Tim Peters2a799bf2002-12-16 20:18:38 +00003925 */
3926
Tim Petersa9bc1682003-01-11 03:39:11 +00003927/* Accessor properties. Properties for day, month, and year are inherited
3928 * from date.
Tim Peters2a799bf2002-12-16 20:18:38 +00003929 */
3930
3931static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00003932datetime_hour(PyDateTime_DateTime *self, void *unused)
Tim Peters2a799bf2002-12-16 20:18:38 +00003933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 return PyLong_FromLong(DATE_GET_HOUR(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00003935}
3936
Tim Petersa9bc1682003-01-11 03:39:11 +00003937static PyObject *
3938datetime_minute(PyDateTime_DateTime *self, void *unused)
3939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 return PyLong_FromLong(DATE_GET_MINUTE(self));
Tim Petersa9bc1682003-01-11 03:39:11 +00003941}
3942
3943static PyObject *
3944datetime_second(PyDateTime_DateTime *self, void *unused)
3945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 return PyLong_FromLong(DATE_GET_SECOND(self));
Tim Petersa9bc1682003-01-11 03:39:11 +00003947}
3948
3949static PyObject *
3950datetime_microsecond(PyDateTime_DateTime *self, void *unused)
3951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 return PyLong_FromLong(DATE_GET_MICROSECOND(self));
Tim Petersa9bc1682003-01-11 03:39:11 +00003953}
3954
3955static PyObject *
3956datetime_tzinfo(PyDateTime_DateTime *self, void *unused)
3957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
3959 Py_INCREF(result);
3960 return result;
Tim Petersa9bc1682003-01-11 03:39:11 +00003961}
3962
3963static PyGetSetDef datetime_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 {"hour", (getter)datetime_hour},
3965 {"minute", (getter)datetime_minute},
3966 {"second", (getter)datetime_second},
3967 {"microsecond", (getter)datetime_microsecond},
3968 {"tzinfo", (getter)datetime_tzinfo},
3969 {NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00003970};
3971
3972/*
3973 * Constructors.
Tim Peters2a799bf2002-12-16 20:18:38 +00003974 */
3975
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +00003976static char *datetime_kws[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 "year", "month", "day", "hour", "minute", "second",
3978 "microsecond", "tzinfo", NULL
Tim Peters12bf3392002-12-24 05:41:27 +00003979};
3980
Tim Peters2a799bf2002-12-16 20:18:38 +00003981static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00003982datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
Tim Peters2a799bf2002-12-16 20:18:38 +00003983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 PyObject *self = NULL;
3985 PyObject *state;
3986 int year;
3987 int month;
3988 int day;
3989 int hour = 0;
3990 int minute = 0;
3991 int second = 0;
3992 int usecond = 0;
3993 PyObject *tzinfo = Py_None;
Tim Peters2a799bf2002-12-16 20:18:38 +00003994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 /* Check for invocation from pickle with __getstate__ state */
3996 if (PyTuple_GET_SIZE(args) >= 1 &&
3997 PyTuple_GET_SIZE(args) <= 2 &&
3998 PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
3999 PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
4000 MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
4001 {
4002 PyDateTime_DateTime *me;
4003 char aware;
Tim Peters70533e22003-02-01 04:40:04 +00004004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 if (PyTuple_GET_SIZE(args) == 2) {
4006 tzinfo = PyTuple_GET_ITEM(args, 1);
4007 if (check_tzinfo_subclass(tzinfo) < 0) {
4008 PyErr_SetString(PyExc_TypeError, "bad "
4009 "tzinfo state arg");
4010 return NULL;
4011 }
4012 }
4013 aware = (char)(tzinfo != Py_None);
4014 me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
4015 if (me != NULL) {
4016 char *pdata = PyBytes_AS_STRING(state);
Tim Peters70533e22003-02-01 04:40:04 +00004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
4019 me->hashcode = -1;
4020 me->hastzinfo = aware;
4021 if (aware) {
4022 Py_INCREF(tzinfo);
4023 me->tzinfo = tzinfo;
4024 }
4025 }
4026 return (PyObject *)me;
4027 }
Guido van Rossum177e41a2003-01-30 22:06:23 +00004028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws,
4030 &year, &month, &day, &hour, &minute,
4031 &second, &usecond, &tzinfo)) {
4032 if (check_date_args(year, month, day) < 0)
4033 return NULL;
4034 if (check_time_args(hour, minute, second, usecond) < 0)
4035 return NULL;
4036 if (check_tzinfo_subclass(tzinfo) < 0)
4037 return NULL;
4038 self = new_datetime_ex(year, month, day,
4039 hour, minute, second, usecond,
4040 tzinfo, type);
4041 }
4042 return self;
Tim Peters2a799bf2002-12-16 20:18:38 +00004043}
4044
Tim Petersa9bc1682003-01-11 03:39:11 +00004045/* TM_FUNC is the shared type of localtime() and gmtime(). */
4046typedef struct tm *(*TM_FUNC)(const time_t *timer);
4047
4048/* Internal helper.
4049 * Build datetime from a time_t and a distinct count of microseconds.
4050 * Pass localtime or gmtime for f, to control the interpretation of timet.
4051 */
4052static PyObject *
4053datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyObject *tzinfo)
Tim Petersa9bc1682003-01-11 03:39:11 +00004055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 struct tm *tm;
Tim Petersa9bc1682003-01-11 03:39:11 +00004057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 tm = f(&timet);
Victor Stinner21f58932012-03-14 00:15:40 +01004059 if (tm == NULL) {
4060#ifdef EINVAL
4061 if (errno == 0)
4062 errno = EINVAL;
4063#endif
4064 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
Victor Stinner21f58932012-03-14 00:15:40 +01004066
4067 /* The platform localtime/gmtime may insert leap seconds,
4068 * indicated by tm->tm_sec > 59. We don't care about them,
4069 * except to the extent that passing them on to the datetime
4070 * constructor would raise ValueError for a reason that
4071 * made no sense to the user.
4072 */
4073 if (tm->tm_sec > 59)
4074 tm->tm_sec = 59;
4075 return PyObject_CallFunction(cls, "iiiiiiiO",
4076 tm->tm_year + 1900,
4077 tm->tm_mon + 1,
4078 tm->tm_mday,
4079 tm->tm_hour,
4080 tm->tm_min,
4081 tm->tm_sec,
4082 us,
4083 tzinfo);
Tim Petersa9bc1682003-01-11 03:39:11 +00004084}
4085
4086/* Internal helper.
4087 * Build datetime from a Python timestamp. Pass localtime or gmtime for f,
4088 * to control the interpretation of the timestamp. Since a double doesn't
4089 * have enough bits to cover a datetime's full range of precision, it's
4090 * better to call datetime_from_timet_and_us provided you have a way
4091 * to get that much precision (e.g., C time() isn't good enough).
4092 */
4093static PyObject *
Victor Stinner5d272cc2012-03-13 13:35:55 +01004094datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 PyObject *tzinfo)
Tim Petersa9bc1682003-01-11 03:39:11 +00004096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 time_t timet;
Victor Stinner5d272cc2012-03-13 13:35:55 +01004098 long us;
Tim Petersa9bc1682003-01-11 03:39:11 +00004099
Victor Stinnere4a994d2015-03-30 01:10:14 +02004100 if (_PyTime_ObjectToTimeval(timestamp,
4101 &timet, &us, _PyTime_ROUND_FLOOR) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 return NULL;
Victor Stinner09e5cf22015-03-30 00:09:18 +02004103 assert(0 <= us && us <= 999999);
4104
Victor Stinner21f58932012-03-14 00:15:40 +01004105 return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
Tim Petersa9bc1682003-01-11 03:39:11 +00004106}
4107
4108/* Internal helper.
4109 * Build most accurate possible datetime for current time. Pass localtime or
4110 * gmtime for f as appropriate.
4111 */
4112static PyObject *
4113datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
4114{
Victor Stinner09e5cf22015-03-30 00:09:18 +02004115 _PyTime_t ts = _PyTime_GetSystemClock();
4116 struct timeval tv;
4117
4118 if (_PyTime_AsTimeval(ts, &tv, _PyTime_ROUND_FLOOR) < 0)
4119 return NULL;
4120 assert(0 <= tv.tv_usec && tv.tv_usec <= 999999);
4121
4122 return datetime_from_timet_and_us(cls, f, tv.tv_sec, tv.tv_usec, tzinfo);
Tim Petersa9bc1682003-01-11 03:39:11 +00004123}
4124
Larry Hastings61272b72014-01-07 12:41:53 -08004125/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07004126
4127@classmethod
Larry Hastingsed4a1c52013-11-18 09:32:13 -08004128datetime.datetime.now
Larry Hastings31826802013-10-19 00:09:25 -07004129
4130 tz: object = None
4131 Timezone object.
4132
4133Returns new datetime object representing current time local to tz.
4134
4135If no tz is specified, uses local timezone.
Larry Hastings61272b72014-01-07 12:41:53 -08004136[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07004137
Larry Hastings31826802013-10-19 00:09:25 -07004138static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08004139datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004140/*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/
Tim Peters2a799bf2002-12-16 20:18:38 +00004141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 PyObject *self;
Tim Peters2a799bf2002-12-16 20:18:38 +00004143
Larry Hastings31826802013-10-19 00:09:25 -07004144 /* Return best possible local time -- this isn't constrained by the
4145 * precision of a timestamp.
4146 */
4147 if (check_tzinfo_subclass(tz) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 return NULL;
Tim Peters10cadce2003-01-23 19:58:02 +00004149
Larry Hastings5c661892014-01-24 06:17:25 -08004150 self = datetime_best_possible((PyObject *)type,
Larry Hastings31826802013-10-19 00:09:25 -07004151 tz == Py_None ? localtime : gmtime,
4152 tz);
4153 if (self != NULL && tz != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 /* Convert UTC to tzinfo's zone. */
4155 PyObject *temp = self;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004156
Larry Hastings31826802013-10-19 00:09:25 -07004157 self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 Py_DECREF(temp);
4159 }
4160 return self;
Tim Peters2a799bf2002-12-16 20:18:38 +00004161}
4162
Tim Petersa9bc1682003-01-11 03:39:11 +00004163/* Return best possible UTC time -- this isn't constrained by the
4164 * precision of a timestamp.
4165 */
4166static PyObject *
4167datetime_utcnow(PyObject *cls, PyObject *dummy)
4168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 return datetime_best_possible(cls, gmtime, Py_None);
Tim Petersa9bc1682003-01-11 03:39:11 +00004170}
4171
Tim Peters2a799bf2002-12-16 20:18:38 +00004172/* Return new local datetime from timestamp (Python timestamp -- a double). */
4173static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004174datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
Tim Peters2a799bf2002-12-16 20:18:38 +00004175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 PyObject *self;
Victor Stinner5d272cc2012-03-13 13:35:55 +01004177 PyObject *timestamp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 PyObject *tzinfo = Py_None;
4179 static char *keywords[] = {"timestamp", "tz", NULL};
Tim Peters2a799bf2002-12-16 20:18:38 +00004180
Victor Stinner5d272cc2012-03-13 13:35:55 +01004181 if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 keywords, &timestamp, &tzinfo))
4183 return NULL;
4184 if (check_tzinfo_subclass(tzinfo) < 0)
4185 return NULL;
Tim Peters2a44a8d2003-01-23 20:53:10 +00004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 self = datetime_from_timestamp(cls,
4188 tzinfo == Py_None ? localtime : gmtime,
4189 timestamp,
4190 tzinfo);
4191 if (self != NULL && tzinfo != Py_None) {
4192 /* Convert UTC to tzinfo's zone. */
4193 PyObject *temp = self;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004194
4195 self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 Py_DECREF(temp);
4197 }
4198 return self;
Tim Peters2a799bf2002-12-16 20:18:38 +00004199}
4200
Tim Petersa9bc1682003-01-11 03:39:11 +00004201/* Return new UTC datetime from timestamp (Python timestamp -- a double). */
4202static PyObject *
4203datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
4204{
Victor Stinner5d272cc2012-03-13 13:35:55 +01004205 PyObject *timestamp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00004207
Victor Stinner5d272cc2012-03-13 13:35:55 +01004208 if (PyArg_ParseTuple(args, "O:utcfromtimestamp", &timestamp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 result = datetime_from_timestamp(cls, gmtime, timestamp,
4210 Py_None);
4211 return result;
Tim Petersa9bc1682003-01-11 03:39:11 +00004212}
4213
Alexander Belopolskyca94f552010-06-17 18:30:34 +00004214/* Return new datetime from _strptime.strptime_datetime(). */
Skip Montanaro0af3ade2005-01-13 04:12:31 +00004215static PyObject *
4216datetime_strptime(PyObject *cls, PyObject *args)
4217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 static PyObject *module = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004219 PyObject *string, *format;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004220 _Py_IDENTIFIER(_strptime_datetime);
Skip Montanaro0af3ade2005-01-13 04:12:31 +00004221
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004222 if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 return NULL;
Skip Montanaro0af3ade2005-01-13 04:12:31 +00004224
Alexander Belopolskyca94f552010-06-17 18:30:34 +00004225 if (module == NULL) {
4226 module = PyImport_ImportModuleNoBlock("_strptime");
Alexander Belopolsky311d2a92010-06-28 14:36:55 +00004227 if (module == NULL)
Alexander Belopolskyca94f552010-06-17 18:30:34 +00004228 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004230 return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO",
4231 cls, string, format);
Skip Montanaro0af3ade2005-01-13 04:12:31 +00004232}
4233
Tim Petersa9bc1682003-01-11 03:39:11 +00004234/* Return new datetime from date/datetime and time arguments. */
4235static PyObject *
4236datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
4237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 static char *keywords[] = {"date", "time", NULL};
4239 PyObject *date;
4240 PyObject *time;
4241 PyObject *result = NULL;
Tim Petersa9bc1682003-01-11 03:39:11 +00004242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords,
4244 &PyDateTime_DateType, &date,
4245 &PyDateTime_TimeType, &time)) {
4246 PyObject *tzinfo = Py_None;
Tim Petersa9bc1682003-01-11 03:39:11 +00004247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 if (HASTZINFO(time))
4249 tzinfo = ((PyDateTime_Time *)time)->tzinfo;
4250 result = PyObject_CallFunction(cls, "iiiiiiiO",
4251 GET_YEAR(date),
4252 GET_MONTH(date),
4253 GET_DAY(date),
4254 TIME_GET_HOUR(time),
4255 TIME_GET_MINUTE(time),
4256 TIME_GET_SECOND(time),
4257 TIME_GET_MICROSECOND(time),
4258 tzinfo);
4259 }
4260 return result;
Tim Petersa9bc1682003-01-11 03:39:11 +00004261}
Tim Peters2a799bf2002-12-16 20:18:38 +00004262
4263/*
4264 * Destructor.
4265 */
4266
4267static void
Tim Petersa9bc1682003-01-11 03:39:11 +00004268datetime_dealloc(PyDateTime_DateTime *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00004269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 if (HASTZINFO(self)) {
4271 Py_XDECREF(self->tzinfo);
4272 }
4273 Py_TYPE(self)->tp_free((PyObject *)self);
Tim Peters2a799bf2002-12-16 20:18:38 +00004274}
4275
4276/*
4277 * Indirect access to tzinfo methods.
4278 */
4279
Tim Peters2a799bf2002-12-16 20:18:38 +00004280/* These are all METH_NOARGS, so don't need to check the arglist. */
4281static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004282datetime_utcoffset(PyObject *self, PyObject *unused) {
4283 return call_utcoffset(GET_DT_TZINFO(self), self);
Tim Peters2a799bf2002-12-16 20:18:38 +00004284}
4285
4286static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004287datetime_dst(PyObject *self, PyObject *unused) {
4288 return call_dst(GET_DT_TZINFO(self), self);
Tim Peters855fe882002-12-22 03:43:39 +00004289}
4290
4291static PyObject *
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004292datetime_tzname(PyObject *self, PyObject *unused) {
4293 return call_tzname(GET_DT_TZINFO(self), self);
Tim Peters2a799bf2002-12-16 20:18:38 +00004294}
4295
4296/*
Tim Petersa9bc1682003-01-11 03:39:11 +00004297 * datetime arithmetic.
Tim Peters2a799bf2002-12-16 20:18:38 +00004298 */
4299
Tim Petersa9bc1682003-01-11 03:39:11 +00004300/* factor must be 1 (to add) or -1 (to subtract). The result inherits
4301 * the tzinfo state of date.
Tim Peters2a799bf2002-12-16 20:18:38 +00004302 */
4303static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004304add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 int factor)
Tim Peters2a799bf2002-12-16 20:18:38 +00004306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 /* Note that the C-level additions can't overflow, because of
4308 * invariant bounds on the member values.
4309 */
4310 int year = GET_YEAR(date);
4311 int month = GET_MONTH(date);
4312 int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor;
4313 int hour = DATE_GET_HOUR(date);
4314 int minute = DATE_GET_MINUTE(date);
4315 int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor;
4316 int microsecond = DATE_GET_MICROSECOND(date) +
4317 GET_TD_MICROSECONDS(delta) * factor;
Tim Peters2a799bf2002-12-16 20:18:38 +00004318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 assert(factor == 1 || factor == -1);
4320 if (normalize_datetime(&year, &month, &day,
4321 &hour, &minute, &second, &microsecond) < 0)
4322 return NULL;
4323 else
4324 return new_datetime(year, month, day,
4325 hour, minute, second, microsecond,
4326 HASTZINFO(date) ? date->tzinfo : Py_None);
Tim Peters2a799bf2002-12-16 20:18:38 +00004327}
4328
4329static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004330datetime_add(PyObject *left, PyObject *right)
Tim Peters2a799bf2002-12-16 20:18:38 +00004331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 if (PyDateTime_Check(left)) {
4333 /* datetime + ??? */
4334 if (PyDelta_Check(right))
4335 /* datetime + delta */
4336 return add_datetime_timedelta(
4337 (PyDateTime_DateTime *)left,
4338 (PyDateTime_Delta *)right,
4339 1);
4340 }
4341 else if (PyDelta_Check(left)) {
4342 /* delta + datetime */
4343 return add_datetime_timedelta((PyDateTime_DateTime *) right,
4344 (PyDateTime_Delta *) left,
4345 1);
4346 }
Brian Curtindfc80e32011-08-10 20:28:54 -05004347 Py_RETURN_NOTIMPLEMENTED;
Tim Peters2a799bf2002-12-16 20:18:38 +00004348}
4349
4350static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004351datetime_subtract(PyObject *left, PyObject *right)
Tim Peters2a799bf2002-12-16 20:18:38 +00004352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 PyObject *result = Py_NotImplemented;
Tim Peters2a799bf2002-12-16 20:18:38 +00004354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 if (PyDateTime_Check(left)) {
4356 /* datetime - ??? */
4357 if (PyDateTime_Check(right)) {
4358 /* datetime - datetime */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004359 PyObject *offset1, *offset2, *offdiff = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 int delta_d, delta_s, delta_us;
Tim Peters2a799bf2002-12-16 20:18:38 +00004361
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004362 if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) {
4363 offset2 = offset1 = Py_None;
4364 Py_INCREF(offset1);
4365 Py_INCREF(offset2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004367 else {
4368 offset1 = datetime_utcoffset(left, NULL);
4369 if (offset1 == NULL)
4370 return NULL;
4371 offset2 = datetime_utcoffset(right, NULL);
4372 if (offset2 == NULL) {
4373 Py_DECREF(offset1);
4374 return NULL;
4375 }
4376 if ((offset1 != Py_None) != (offset2 != Py_None)) {
4377 PyErr_SetString(PyExc_TypeError,
4378 "can't subtract offset-naive and "
4379 "offset-aware datetimes");
4380 Py_DECREF(offset1);
4381 Py_DECREF(offset2);
4382 return NULL;
4383 }
4384 }
4385 if ((offset1 != offset2) &&
4386 delta_cmp(offset1, offset2) != 0) {
4387 offdiff = delta_subtract(offset1, offset2);
4388 if (offdiff == NULL) {
4389 Py_DECREF(offset1);
4390 Py_DECREF(offset2);
4391 return NULL;
4392 }
4393 }
4394 Py_DECREF(offset1);
4395 Py_DECREF(offset2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 delta_d = ymd_to_ord(GET_YEAR(left),
4397 GET_MONTH(left),
4398 GET_DAY(left)) -
4399 ymd_to_ord(GET_YEAR(right),
4400 GET_MONTH(right),
4401 GET_DAY(right));
4402 /* These can't overflow, since the values are
4403 * normalized. At most this gives the number of
4404 * seconds in one day.
4405 */
4406 delta_s = (DATE_GET_HOUR(left) -
4407 DATE_GET_HOUR(right)) * 3600 +
4408 (DATE_GET_MINUTE(left) -
4409 DATE_GET_MINUTE(right)) * 60 +
4410 (DATE_GET_SECOND(left) -
4411 DATE_GET_SECOND(right));
4412 delta_us = DATE_GET_MICROSECOND(left) -
4413 DATE_GET_MICROSECOND(right);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 result = new_delta(delta_d, delta_s, delta_us, 1);
Victor Stinner70e11ac2013-11-08 00:50:58 +01004415 if (result == NULL)
4416 return NULL;
4417
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004418 if (offdiff != NULL) {
4419 PyObject *temp = result;
4420 result = delta_subtract(result, offdiff);
4421 Py_DECREF(temp);
4422 Py_DECREF(offdiff);
4423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 }
4425 else if (PyDelta_Check(right)) {
4426 /* datetime - delta */
4427 result = add_datetime_timedelta(
4428 (PyDateTime_DateTime *)left,
4429 (PyDateTime_Delta *)right,
4430 -1);
4431 }
4432 }
Tim Peters2a799bf2002-12-16 20:18:38 +00004433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 if (result == Py_NotImplemented)
4435 Py_INCREF(result);
4436 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00004437}
4438
4439/* Various ways to turn a datetime into a string. */
4440
4441static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004442datetime_repr(PyDateTime_DateTime *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00004443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 const char *type_name = Py_TYPE(self)->tp_name;
4445 PyObject *baserepr;
Tim Peters2a799bf2002-12-16 20:18:38 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 if (DATE_GET_MICROSECOND(self)) {
4448 baserepr = PyUnicode_FromFormat(
4449 "%s(%d, %d, %d, %d, %d, %d, %d)",
4450 type_name,
4451 GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
4452 DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
4453 DATE_GET_SECOND(self),
4454 DATE_GET_MICROSECOND(self));
4455 }
4456 else if (DATE_GET_SECOND(self)) {
4457 baserepr = PyUnicode_FromFormat(
4458 "%s(%d, %d, %d, %d, %d, %d)",
4459 type_name,
4460 GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
4461 DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
4462 DATE_GET_SECOND(self));
4463 }
4464 else {
4465 baserepr = PyUnicode_FromFormat(
4466 "%s(%d, %d, %d, %d, %d)",
4467 type_name,
4468 GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
4469 DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
4470 }
4471 if (baserepr == NULL || ! HASTZINFO(self))
4472 return baserepr;
4473 return append_keyword_tzinfo(baserepr, self->tzinfo);
Tim Peters2a799bf2002-12-16 20:18:38 +00004474}
4475
Tim Petersa9bc1682003-01-11 03:39:11 +00004476static PyObject *
4477datetime_str(PyDateTime_DateTime *self)
4478{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004479 return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "(s)", " ");
Tim Petersa9bc1682003-01-11 03:39:11 +00004480}
Tim Peters2a799bf2002-12-16 20:18:38 +00004481
4482static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004483datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
Tim Peters2a799bf2002-12-16 20:18:38 +00004484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 int sep = 'T';
4486 static char *keywords[] = {"sep", NULL};
4487 char buffer[100];
4488 PyObject *result;
4489 int us = DATE_GET_MICROSECOND(self);
Tim Peters2a799bf2002-12-16 20:18:38 +00004490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep))
4492 return NULL;
4493 if (us)
4494 result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d",
4495 GET_YEAR(self), GET_MONTH(self),
4496 GET_DAY(self), (int)sep,
4497 DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
4498 DATE_GET_SECOND(self), us);
4499 else
4500 result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d",
4501 GET_YEAR(self), GET_MONTH(self),
4502 GET_DAY(self), (int)sep,
4503 DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
4504 DATE_GET_SECOND(self));
Walter Dörwaldbafa1372007-05-31 17:50:48 +00004505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 if (!result || !HASTZINFO(self))
4507 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 /* We need to append the UTC offset. */
4510 if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo,
4511 (PyObject *)self) < 0) {
4512 Py_DECREF(result);
4513 return NULL;
4514 }
4515 PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer));
4516 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00004517}
4518
Tim Petersa9bc1682003-01-11 03:39:11 +00004519static PyObject *
4520datetime_ctime(PyDateTime_DateTime *self)
4521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 return format_ctime((PyDateTime_Date *)self,
4523 DATE_GET_HOUR(self),
4524 DATE_GET_MINUTE(self),
4525 DATE_GET_SECOND(self));
Tim Petersa9bc1682003-01-11 03:39:11 +00004526}
4527
Tim Peters2a799bf2002-12-16 20:18:38 +00004528/* Miscellaneous methods. */
4529
Tim Petersa9bc1682003-01-11 03:39:11 +00004530static PyObject *
Guido van Rossum19960592006-08-24 17:29:38 +00004531datetime_richcompare(PyObject *self, PyObject *other, int op)
Tim Petersa9bc1682003-01-11 03:39:11 +00004532{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004533 PyObject *result = NULL;
4534 PyObject *offset1, *offset2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 int diff;
Tim Petersa9bc1682003-01-11 03:39:11 +00004536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 if (! PyDateTime_Check(other)) {
4538 if (PyDate_Check(other)) {
4539 /* Prevent invocation of date_richcompare. We want to
4540 return NotImplemented here to give the other object
4541 a chance. But since DateTime is a subclass of
4542 Date, if the other object is a Date, it would
4543 compute an ordering based on the date part alone,
4544 and we don't want that. So force unequal or
4545 uncomparable here in that case. */
4546 if (op == Py_EQ)
4547 Py_RETURN_FALSE;
4548 if (op == Py_NE)
4549 Py_RETURN_TRUE;
4550 return cmperror(self, other);
4551 }
Brian Curtindfc80e32011-08-10 20:28:54 -05004552 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 }
Tim Petersa9bc1682003-01-11 03:39:11 +00004554
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004555 if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 diff = memcmp(((PyDateTime_DateTime *)self)->data,
4557 ((PyDateTime_DateTime *)other)->data,
4558 _PyDateTime_DATETIME_DATASIZE);
4559 return diff_to_bool(diff, op);
4560 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004561 offset1 = datetime_utcoffset(self, NULL);
4562 if (offset1 == NULL)
4563 return NULL;
4564 offset2 = datetime_utcoffset(other, NULL);
4565 if (offset2 == NULL)
4566 goto done;
4567 /* If they're both naive, or both aware and have the same offsets,
4568 * we get off cheap. Note that if they're both naive, offset1 ==
4569 * offset2 == Py_None at this point.
4570 */
4571 if ((offset1 == offset2) ||
4572 (PyDelta_Check(offset1) && PyDelta_Check(offset2) &&
4573 delta_cmp(offset1, offset2) == 0)) {
4574 diff = memcmp(((PyDateTime_DateTime *)self)->data,
4575 ((PyDateTime_DateTime *)other)->data,
4576 _PyDateTime_DATETIME_DATASIZE);
4577 result = diff_to_bool(diff, op);
4578 }
4579 else if (offset1 != Py_None && offset2 != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 PyDateTime_Delta *delta;
Tim Petersa9bc1682003-01-11 03:39:11 +00004581
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004582 assert(offset1 != offset2); /* else last "if" handled it */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self,
4584 other);
4585 if (delta == NULL)
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004586 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 diff = GET_TD_DAYS(delta);
4588 if (diff == 0)
4589 diff = GET_TD_SECONDS(delta) |
4590 GET_TD_MICROSECONDS(delta);
4591 Py_DECREF(delta);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004592 result = diff_to_bool(diff, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 }
Alexander Belopolsky08313822012-06-15 20:19:47 -04004594 else if (op == Py_EQ) {
4595 result = Py_False;
4596 Py_INCREF(result);
4597 }
4598 else if (op == Py_NE) {
4599 result = Py_True;
4600 Py_INCREF(result);
4601 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004602 else {
4603 PyErr_SetString(PyExc_TypeError,
4604 "can't compare offset-naive and "
4605 "offset-aware datetimes");
4606 }
4607 done:
4608 Py_DECREF(offset1);
4609 Py_XDECREF(offset2);
4610 return result;
Tim Petersa9bc1682003-01-11 03:39:11 +00004611}
4612
Benjamin Peterson8f67d082010-10-17 20:54:53 +00004613static Py_hash_t
Tim Petersa9bc1682003-01-11 03:39:11 +00004614datetime_hash(PyDateTime_DateTime *self)
4615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 if (self->hashcode == -1) {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004617 PyObject *offset;
Tim Petersa9bc1682003-01-11 03:39:11 +00004618
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004619 offset = datetime_utcoffset((PyObject *)self, NULL);
4620
4621 if (offset == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 return -1;
Tim Petersa9bc1682003-01-11 03:39:11 +00004623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 /* Reduce this to a hash of another object. */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004625 if (offset == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 self->hashcode = generic_hash(
4627 (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 else {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004629 PyObject *temp1, *temp2;
4630 int days, seconds;
Tim Petersa9bc1682003-01-11 03:39:11 +00004631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 assert(HASTZINFO(self));
4633 days = ymd_to_ord(GET_YEAR(self),
4634 GET_MONTH(self),
4635 GET_DAY(self));
4636 seconds = DATE_GET_HOUR(self) * 3600 +
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004637 DATE_GET_MINUTE(self) * 60 +
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 DATE_GET_SECOND(self);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004639 temp1 = new_delta(days, seconds,
4640 DATE_GET_MICROSECOND(self),
4641 1);
4642 if (temp1 == NULL) {
4643 Py_DECREF(offset);
4644 return -1;
4645 }
4646 temp2 = delta_subtract(temp1, offset);
4647 Py_DECREF(temp1);
4648 if (temp2 == NULL) {
4649 Py_DECREF(offset);
4650 return -1;
4651 }
4652 self->hashcode = PyObject_Hash(temp2);
4653 Py_DECREF(temp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004655 Py_DECREF(offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 }
4657 return self->hashcode;
Tim Petersa9bc1682003-01-11 03:39:11 +00004658}
Tim Peters2a799bf2002-12-16 20:18:38 +00004659
4660static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004661datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
Tim Peters12bf3392002-12-24 05:41:27 +00004662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 PyObject *clone;
4664 PyObject *tuple;
4665 int y = GET_YEAR(self);
4666 int m = GET_MONTH(self);
4667 int d = GET_DAY(self);
4668 int hh = DATE_GET_HOUR(self);
4669 int mm = DATE_GET_MINUTE(self);
4670 int ss = DATE_GET_SECOND(self);
4671 int us = DATE_GET_MICROSECOND(self);
4672 PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
Tim Peters12bf3392002-12-24 05:41:27 +00004673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace",
4675 datetime_kws,
4676 &y, &m, &d, &hh, &mm, &ss, &us,
4677 &tzinfo))
4678 return NULL;
4679 tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
4680 if (tuple == NULL)
4681 return NULL;
4682 clone = datetime_new(Py_TYPE(self), tuple, NULL);
4683 Py_DECREF(tuple);
4684 return clone;
Tim Peters12bf3392002-12-24 05:41:27 +00004685}
4686
4687static PyObject *
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004688local_timezone(PyDateTime_DateTime *utc_time)
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004689{
4690 PyObject *result = NULL;
4691 struct tm *timep;
4692 time_t timestamp;
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004693 PyObject *delta;
4694 PyObject *one_second;
4695 PyObject *seconds;
4696 PyObject *nameo = NULL;
4697 const char *zone = NULL;
4698
4699 delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch);
4700 if (delta == NULL)
4701 return NULL;
4702 one_second = new_delta(0, 1, 0, 0);
4703 if (one_second == NULL)
4704 goto error;
4705 seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta,
4706 (PyDateTime_Delta *)one_second);
4707 Py_DECREF(one_second);
4708 if (seconds == NULL)
4709 goto error;
4710 Py_DECREF(delta);
4711 timestamp = PyLong_AsLong(seconds);
4712 Py_DECREF(seconds);
4713 if (timestamp == -1 && PyErr_Occurred())
4714 return NULL;
4715 timep = localtime(&timestamp);
4716#ifdef HAVE_STRUCT_TM_TM_ZONE
Alexander Belopolsky93c9cd02012-06-22 16:04:19 -04004717 zone = timep->tm_zone;
4718 delta = new_delta(0, timep->tm_gmtoff, 0, 1);
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004719#else /* HAVE_STRUCT_TM_TM_ZONE */
4720 {
4721 PyObject *local_time;
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004722 local_time = new_datetime(timep->tm_year + 1900, timep->tm_mon + 1,
4723 timep->tm_mday, timep->tm_hour, timep->tm_min,
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004724 timep->tm_sec, DATE_GET_MICROSECOND(utc_time),
4725 utc_time->tzinfo);
4726 if (local_time == NULL)
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004727 goto error;
Alexander Belopolsky93c9cd02012-06-22 16:04:19 -04004728 delta = datetime_subtract(local_time, (PyObject*)utc_time);
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004729 /* XXX: before relying on tzname, we should compare delta
4730 to the offset implied by timezone/altzone */
4731 if (daylight && timep->tm_isdst >= 0)
4732 zone = tzname[timep->tm_isdst % 2];
4733 else
4734 zone = tzname[0];
4735 Py_DECREF(local_time);
4736 }
4737#endif /* HAVE_STRUCT_TM_TM_ZONE */
4738 if (zone != NULL) {
4739 nameo = PyUnicode_DecodeLocale(zone, "surrogateescape");
4740 if (nameo == NULL)
4741 goto error;
4742 }
4743 result = new_timezone(delta, nameo);
Christian Heimesb91ffaa2013-06-29 20:52:33 +02004744 Py_XDECREF(nameo);
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004745 error:
4746 Py_DECREF(delta);
4747 return result;
4748}
4749
Alexander Belopolsky878054e2012-06-22 14:11:58 -04004750static PyDateTime_DateTime *
Tim Petersa9bc1682003-01-11 03:39:11 +00004751datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
Tim Peters80475bb2002-12-25 07:40:55 +00004752{
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004753 PyDateTime_DateTime *result;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004754 PyObject *offset;
4755 PyObject *temp;
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004756 PyObject *tzinfo = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004757 static char *keywords[] = {"tz", NULL};
Tim Peters80475bb2002-12-25 07:40:55 +00004758
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004759 if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords,
Raymond Hettinger5a2146a2014-07-25 14:59:48 -07004760 &tzinfo))
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004761 return NULL;
4762
4763 if (check_tzinfo_subclass(tzinfo) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 return NULL;
Tim Peters80475bb2002-12-25 07:40:55 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 if (!HASTZINFO(self) || self->tzinfo == Py_None)
4767 goto NeedAware;
Tim Peters521fc152002-12-31 17:36:56 +00004768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 /* Conversion to self's own time zone is a NOP. */
4770 if (self->tzinfo == tzinfo) {
4771 Py_INCREF(self);
Alexander Belopolsky878054e2012-06-22 14:11:58 -04004772 return self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 }
Tim Peters521fc152002-12-31 17:36:56 +00004774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 /* Convert self to UTC. */
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004776 offset = datetime_utcoffset((PyObject *)self, NULL);
4777 if (offset == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 return NULL;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004779 if (offset == Py_None) {
4780 Py_DECREF(offset);
4781 NeedAware:
4782 PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to "
4783 "a naive datetime");
4784 return NULL;
4785 }
Tim Petersf3615152003-01-01 21:51:37 +00004786
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004787 /* result = self - offset */
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004788 result = (PyDateTime_DateTime *)add_datetime_timedelta(self,
4789 (PyDateTime_Delta *)offset, -1);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004790 Py_DECREF(offset);
4791 if (result == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 return NULL;
Tim Peters52dcce22003-01-23 16:36:11 +00004793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 /* Attach new tzinfo and let fromutc() do the rest. */
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004795 temp = result->tzinfo;
Alexander Belopolskyfdc860f2012-06-22 12:23:23 -04004796 if (tzinfo == Py_None) {
4797 tzinfo = local_timezone(result);
4798 if (tzinfo == NULL) {
4799 Py_DECREF(result);
4800 return NULL;
4801 }
4802 }
4803 else
4804 Py_INCREF(tzinfo);
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004805 result->tzinfo = tzinfo;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004806 Py_DECREF(temp);
Tim Peters52dcce22003-01-23 16:36:11 +00004807
Alexander Belopolsky31227ca2012-06-22 13:23:21 -04004808 temp = (PyObject *)result;
Alexander Belopolsky878054e2012-06-22 14:11:58 -04004809 result = (PyDateTime_DateTime *)
4810 _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp);
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004811 Py_DECREF(temp);
4812
Alexander Belopolsky878054e2012-06-22 14:11:58 -04004813 return result;
Tim Peters80475bb2002-12-25 07:40:55 +00004814}
4815
4816static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004817datetime_timetuple(PyDateTime_DateTime *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00004818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 int dstflag = -1;
Tim Peters2a799bf2002-12-16 20:18:38 +00004820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 if (HASTZINFO(self) && self->tzinfo != Py_None) {
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004822 PyObject * dst;
Tim Peters2a799bf2002-12-16 20:18:38 +00004823
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004824 dst = call_dst(self->tzinfo, (PyObject *)self);
4825 if (dst == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00004827
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004828 if (dst != Py_None)
4829 dstflag = delta_bool((PyDateTime_Delta *)dst);
4830 Py_DECREF(dst);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 }
4832 return build_struct_time(GET_YEAR(self),
4833 GET_MONTH(self),
4834 GET_DAY(self),
4835 DATE_GET_HOUR(self),
4836 DATE_GET_MINUTE(self),
4837 DATE_GET_SECOND(self),
4838 dstflag);
Tim Peters2a799bf2002-12-16 20:18:38 +00004839}
4840
4841static PyObject *
Alexander Belopolskya4415142012-06-08 12:33:09 -04004842datetime_timestamp(PyDateTime_DateTime *self)
4843{
4844 PyObject *result;
4845
4846 if (HASTZINFO(self) && self->tzinfo != Py_None) {
4847 PyObject *delta;
4848 delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch);
4849 if (delta == NULL)
4850 return NULL;
4851 result = delta_total_seconds(delta);
4852 Py_DECREF(delta);
4853 }
4854 else {
4855 struct tm time;
4856 time_t timestamp;
4857 memset((void *) &time, '\0', sizeof(struct tm));
4858 time.tm_year = GET_YEAR(self) - 1900;
4859 time.tm_mon = GET_MONTH(self) - 1;
4860 time.tm_mday = GET_DAY(self);
4861 time.tm_hour = DATE_GET_HOUR(self);
4862 time.tm_min = DATE_GET_MINUTE(self);
4863 time.tm_sec = DATE_GET_SECOND(self);
4864 time.tm_wday = -1;
4865 time.tm_isdst = -1;
4866 timestamp = mktime(&time);
Victor Stinner93037492013-06-25 22:54:35 +02004867 if (timestamp == (time_t)(-1)
4868#ifndef _AIX
4869 /* Return value of -1 does not necessarily mean an error,
4870 * but tm_wday cannot remain set to -1 if mktime succeeded. */
4871 && time.tm_wday == -1
4872#else
4873 /* on AIX, tm_wday is always sets, even on error */
4874#endif
4875 )
4876 {
Alexander Belopolskya4415142012-06-08 12:33:09 -04004877 PyErr_SetString(PyExc_OverflowError,
4878 "timestamp out of range");
4879 return NULL;
4880 }
4881 result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6);
4882 }
4883 return result;
4884}
4885
4886static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004887datetime_getdate(PyDateTime_DateTime *self)
4888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 return new_date(GET_YEAR(self),
4890 GET_MONTH(self),
4891 GET_DAY(self));
Tim Petersa9bc1682003-01-11 03:39:11 +00004892}
4893
4894static PyObject *
4895datetime_gettime(PyDateTime_DateTime *self)
4896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004897 return new_time(DATE_GET_HOUR(self),
4898 DATE_GET_MINUTE(self),
4899 DATE_GET_SECOND(self),
4900 DATE_GET_MICROSECOND(self),
4901 Py_None);
Tim Petersa9bc1682003-01-11 03:39:11 +00004902}
4903
4904static PyObject *
4905datetime_gettimetz(PyDateTime_DateTime *self)
4906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 return new_time(DATE_GET_HOUR(self),
4908 DATE_GET_MINUTE(self),
4909 DATE_GET_SECOND(self),
4910 DATE_GET_MICROSECOND(self),
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004911 GET_DT_TZINFO(self));
Tim Petersa9bc1682003-01-11 03:39:11 +00004912}
4913
4914static PyObject *
4915datetime_utctimetuple(PyDateTime_DateTime *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00004916{
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004917 int y, m, d, hh, mm, ss;
4918 PyObject *tzinfo;
4919 PyDateTime_DateTime *utcself;
Tim Peters2a799bf2002-12-16 20:18:38 +00004920
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004921 tzinfo = GET_DT_TZINFO(self);
4922 if (tzinfo == Py_None) {
4923 utcself = self;
4924 Py_INCREF(utcself);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004926 else {
4927 PyObject *offset;
4928 offset = call_utcoffset(tzinfo, (PyObject *)self);
4929 if (offset == NULL)
Alexander Belopolsky75f94c22010-06-21 15:21:14 +00004930 return NULL;
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004931 if (offset == Py_None) {
4932 Py_DECREF(offset);
4933 utcself = self;
4934 Py_INCREF(utcself);
4935 }
4936 else {
4937 utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self,
4938 (PyDateTime_Delta *)offset, -1);
4939 Py_DECREF(offset);
4940 if (utcself == NULL)
4941 return NULL;
4942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 }
Alexander Belopolsky73ca4402010-07-07 23:56:38 +00004944 y = GET_YEAR(utcself);
4945 m = GET_MONTH(utcself);
4946 d = GET_DAY(utcself);
4947 hh = DATE_GET_HOUR(utcself);
4948 mm = DATE_GET_MINUTE(utcself);
4949 ss = DATE_GET_SECOND(utcself);
4950
4951 Py_DECREF(utcself);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 return build_struct_time(y, m, d, hh, mm, ss, 0);
Tim Peters2a799bf2002-12-16 20:18:38 +00004953}
4954
Tim Peters371935f2003-02-01 01:52:50 +00004955/* Pickle support, a simple use of __reduce__. */
Tim Peters33e0f382003-01-10 02:05:14 +00004956
Tim Petersa9bc1682003-01-11 03:39:11 +00004957/* Let basestate be the non-tzinfo data string.
Tim Peters2a799bf2002-12-16 20:18:38 +00004958 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
4959 * So it's a tuple in any (non-error) case.
Tim Petersb57f8f02003-02-01 02:54:15 +00004960 * __getstate__ isn't exposed.
Tim Peters2a799bf2002-12-16 20:18:38 +00004961 */
4962static PyObject *
Tim Petersa9bc1682003-01-11 03:39:11 +00004963datetime_getstate(PyDateTime_DateTime *self)
Tim Peters2a799bf2002-12-16 20:18:38 +00004964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004965 PyObject *basestate;
4966 PyObject *result = NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00004967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 basestate = PyBytes_FromStringAndSize((char *)self->data,
4969 _PyDateTime_DATETIME_DATASIZE);
4970 if (basestate != NULL) {
4971 if (! HASTZINFO(self) || self->tzinfo == Py_None)
4972 result = PyTuple_Pack(1, basestate);
4973 else
4974 result = PyTuple_Pack(2, basestate, self->tzinfo);
4975 Py_DECREF(basestate);
4976 }
4977 return result;
Tim Peters2a799bf2002-12-16 20:18:38 +00004978}
4979
4980static PyObject *
Guido van Rossum177e41a2003-01-30 22:06:23 +00004981datetime_reduce(PyDateTime_DateTime *self, PyObject *arg)
Tim Peters2a799bf2002-12-16 20:18:38 +00004982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self));
Tim Peters2a799bf2002-12-16 20:18:38 +00004984}
4985
Tim Petersa9bc1682003-01-11 03:39:11 +00004986static PyMethodDef datetime_methods[] = {
Guido van Rossum177e41a2003-01-30 22:06:23 +00004987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004988 /* Class methods: */
Tim Peters2a799bf2002-12-16 20:18:38 +00004989
Larry Hastingsed4a1c52013-11-18 09:32:13 -08004990 DATETIME_DATETIME_NOW_METHODDEF
Tim Peters2a799bf2002-12-16 20:18:38 +00004991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 {"utcnow", (PyCFunction)datetime_utcnow,
4993 METH_NOARGS | METH_CLASS,
4994 PyDoc_STR("Return a new datetime representing UTC day and time.")},
Tim Petersa9bc1682003-01-11 03:39:11 +00004995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 {"fromtimestamp", (PyCFunction)datetime_fromtimestamp,
4997 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
4998 PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00004999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
5001 METH_VARARGS | METH_CLASS,
Alexander Belopolskye2e178e2015-03-01 14:52:07 -05005002 PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")},
Tim Petersa9bc1682003-01-11 03:39:11 +00005003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 {"strptime", (PyCFunction)datetime_strptime,
5005 METH_VARARGS | METH_CLASS,
5006 PyDoc_STR("string, format -> new datetime parsed from a string "
5007 "(like time.strptime()).")},
Skip Montanaro0af3ade2005-01-13 04:12:31 +00005008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 {"combine", (PyCFunction)datetime_combine,
5010 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
5011 PyDoc_STR("date, time -> datetime with same date and time fields")},
Tim Petersa9bc1682003-01-11 03:39:11 +00005012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 /* Instance methods: */
Guido van Rossum177e41a2003-01-30 22:06:23 +00005014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005015 {"date", (PyCFunction)datetime_getdate, METH_NOARGS,
5016 PyDoc_STR("Return date object with same year, month and day.")},
Tim Petersa9bc1682003-01-11 03:39:11 +00005017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 {"time", (PyCFunction)datetime_gettime, METH_NOARGS,
5019 PyDoc_STR("Return time object with same time but with tzinfo=None.")},
Tim Petersa9bc1682003-01-11 03:39:11 +00005020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS,
5022 PyDoc_STR("Return time object with same time and tzinfo.")},
Tim Petersa9bc1682003-01-11 03:39:11 +00005023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005024 {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS,
5025 PyDoc_STR("Return ctime() style string.")},
Tim Petersa9bc1682003-01-11 03:39:11 +00005026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS,
5028 PyDoc_STR("Return time tuple, compatible with time.localtime().")},
Tim Peters2a799bf2002-12-16 20:18:38 +00005029
Alexander Belopolskya4415142012-06-08 12:33:09 -04005030 {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS,
5031 PyDoc_STR("Return POSIX timestamp as float.")},
5032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS,
5034 PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")},
Tim Peters2a799bf2002-12-16 20:18:38 +00005035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS,
5037 PyDoc_STR("[sep] -> string in ISO 8601 format, "
5038 "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n"
5039 "sep is used to separate the year from the time, and "
5040 "defaults to 'T'.")},
Tim Peters2a799bf2002-12-16 20:18:38 +00005041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS,
5043 PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00005044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS,
5046 PyDoc_STR("Return self.tzinfo.tzname(self).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00005047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 {"dst", (PyCFunction)datetime_dst, METH_NOARGS,
5049 PyDoc_STR("Return self.tzinfo.dst(self).")},
Tim Peters2a799bf2002-12-16 20:18:38 +00005050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS,
5052 PyDoc_STR("Return datetime with new specified fields.")},
Tim Peters12bf3392002-12-24 05:41:27 +00005053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS,
5055 PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
Tim Peters80475bb2002-12-25 07:40:55 +00005056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS,
5058 PyDoc_STR("__reduce__() -> (cls, state)")},
Guido van Rossum177e41a2003-01-30 22:06:23 +00005059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 {NULL, NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00005061};
5062
Tim Petersa9bc1682003-01-11 03:39:11 +00005063static char datetime_doc[] =
Raymond Hettinger3a4231d2004-12-19 20:13:24 +00005064PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\
5065\n\
5066The year, month and day arguments are required. tzinfo may be None, or an\n\
Serhiy Storchaka95949422013-08-27 19:40:23 +03005067instance of a tzinfo subclass. The remaining arguments may be ints.\n");
Tim Peters2a799bf2002-12-16 20:18:38 +00005068
Tim Petersa9bc1682003-01-11 03:39:11 +00005069static PyNumberMethods datetime_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 datetime_add, /* nb_add */
5071 datetime_subtract, /* nb_subtract */
5072 0, /* nb_multiply */
5073 0, /* nb_remainder */
5074 0, /* nb_divmod */
5075 0, /* nb_power */
5076 0, /* nb_negative */
5077 0, /* nb_positive */
5078 0, /* nb_absolute */
5079 0, /* nb_bool */
Tim Peters2a799bf2002-12-16 20:18:38 +00005080};
5081
Neal Norwitz227b5332006-03-22 09:28:35 +00005082static PyTypeObject PyDateTime_DateTimeType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 PyVarObject_HEAD_INIT(NULL, 0)
5084 "datetime.datetime", /* tp_name */
5085 sizeof(PyDateTime_DateTime), /* tp_basicsize */
5086 0, /* tp_itemsize */
5087 (destructor)datetime_dealloc, /* tp_dealloc */
5088 0, /* tp_print */
5089 0, /* tp_getattr */
5090 0, /* tp_setattr */
5091 0, /* tp_reserved */
5092 (reprfunc)datetime_repr, /* tp_repr */
5093 &datetime_as_number, /* tp_as_number */
5094 0, /* tp_as_sequence */
5095 0, /* tp_as_mapping */
5096 (hashfunc)datetime_hash, /* tp_hash */
5097 0, /* tp_call */
5098 (reprfunc)datetime_str, /* tp_str */
5099 PyObject_GenericGetAttr, /* tp_getattro */
5100 0, /* tp_setattro */
5101 0, /* tp_as_buffer */
5102 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5103 datetime_doc, /* tp_doc */
5104 0, /* tp_traverse */
5105 0, /* tp_clear */
5106 datetime_richcompare, /* tp_richcompare */
5107 0, /* tp_weaklistoffset */
5108 0, /* tp_iter */
5109 0, /* tp_iternext */
5110 datetime_methods, /* tp_methods */
5111 0, /* tp_members */
5112 datetime_getset, /* tp_getset */
5113 &PyDateTime_DateType, /* tp_base */
5114 0, /* tp_dict */
5115 0, /* tp_descr_get */
5116 0, /* tp_descr_set */
5117 0, /* tp_dictoffset */
5118 0, /* tp_init */
5119 datetime_alloc, /* tp_alloc */
5120 datetime_new, /* tp_new */
5121 0, /* tp_free */
Tim Peters2a799bf2002-12-16 20:18:38 +00005122};
5123
5124/* ---------------------------------------------------------------------------
5125 * Module methods and initialization.
5126 */
5127
5128static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 {NULL, NULL}
Tim Peters2a799bf2002-12-16 20:18:38 +00005130};
5131
Tim Peters9ddf40b2004-06-20 22:41:32 +00005132/* C API. Clients get at this via PyDateTime_IMPORT, defined in
5133 * datetime.h.
5134 */
5135static PyDateTime_CAPI CAPI = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 &PyDateTime_DateType,
5137 &PyDateTime_DateTimeType,
5138 &PyDateTime_TimeType,
5139 &PyDateTime_DeltaType,
5140 &PyDateTime_TZInfoType,
5141 new_date_ex,
5142 new_datetime_ex,
5143 new_time_ex,
5144 new_delta_ex,
5145 datetime_fromtimestamp,
5146 date_fromtimestamp
Tim Peters9ddf40b2004-06-20 22:41:32 +00005147};
5148
5149
Martin v. Löwis1a214512008-06-11 05:26:20 +00005150
5151static struct PyModuleDef datetimemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 PyModuleDef_HEAD_INIT,
Alexander Belopolskycf86e362010-07-23 19:25:47 +00005153 "_datetime",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 "Fast implementation of the datetime type.",
5155 -1,
5156 module_methods,
5157 NULL,
5158 NULL,
5159 NULL,
5160 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005161};
5162
Tim Peters2a799bf2002-12-16 20:18:38 +00005163PyMODINIT_FUNC
Alexander Belopolskycf86e362010-07-23 19:25:47 +00005164PyInit__datetime(void)
Tim Peters2a799bf2002-12-16 20:18:38 +00005165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005166 PyObject *m; /* a module object */
5167 PyObject *d; /* its dict */
5168 PyObject *x;
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005169 PyObject *delta;
Tim Peters2a799bf2002-12-16 20:18:38 +00005170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 m = PyModule_Create(&datetimemodule);
5172 if (m == NULL)
5173 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00005174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005175 if (PyType_Ready(&PyDateTime_DateType) < 0)
5176 return NULL;
5177 if (PyType_Ready(&PyDateTime_DateTimeType) < 0)
5178 return NULL;
5179 if (PyType_Ready(&PyDateTime_DeltaType) < 0)
5180 return NULL;
5181 if (PyType_Ready(&PyDateTime_TimeType) < 0)
5182 return NULL;
5183 if (PyType_Ready(&PyDateTime_TZInfoType) < 0)
5184 return NULL;
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005185 if (PyType_Ready(&PyDateTime_TimeZoneType) < 0)
5186 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00005187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 /* timedelta values */
5189 d = PyDateTime_DeltaType.tp_dict;
Tim Peters2a799bf2002-12-16 20:18:38 +00005190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005191 x = new_delta(0, 0, 1, 0);
5192 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
5193 return NULL;
5194 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005196 x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
5197 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
5198 return NULL;
5199 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
5202 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
5203 return NULL;
5204 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 /* date values */
5207 d = PyDateTime_DateType.tp_dict;
Tim Peters2a799bf2002-12-16 20:18:38 +00005208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 x = new_date(1, 1, 1);
5210 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
5211 return NULL;
5212 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005214 x = new_date(MAXYEAR, 12, 31);
5215 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
5216 return NULL;
5217 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 x = new_delta(1, 0, 0, 0);
5220 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
5221 return NULL;
5222 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 /* time values */
5225 d = PyDateTime_TimeType.tp_dict;
Tim Peters2a799bf2002-12-16 20:18:38 +00005226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 x = new_time(0, 0, 0, 0, Py_None);
5228 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
5229 return NULL;
5230 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 x = new_time(23, 59, 59, 999999, Py_None);
5233 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
5234 return NULL;
5235 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 x = new_delta(0, 0, 1, 0);
5238 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
5239 return NULL;
5240 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 /* datetime values */
5243 d = PyDateTime_DateTimeType.tp_dict;
Tim Peters2a799bf2002-12-16 20:18:38 +00005244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None);
5246 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
5247 return NULL;
5248 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None);
5251 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
5252 return NULL;
5253 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 x = new_delta(0, 0, 1, 0);
5256 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
5257 return NULL;
5258 Py_DECREF(x);
Tim Peters2a799bf2002-12-16 20:18:38 +00005259
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005260 /* timezone values */
5261 d = PyDateTime_TimeZoneType.tp_dict;
5262
5263 delta = new_delta(0, 0, 0, 0);
5264 if (delta == NULL)
5265 return NULL;
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +00005266 x = create_timezone(delta, NULL);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005267 Py_DECREF(delta);
5268 if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0)
5269 return NULL;
Alexander Belopolskya11d8c02010-07-06 23:19:45 +00005270 PyDateTime_TimeZone_UTC = x;
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005271
5272 delta = new_delta(-1, 60, 0, 1); /* -23:59 */
5273 if (delta == NULL)
5274 return NULL;
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +00005275 x = create_timezone(delta, NULL);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005276 Py_DECREF(delta);
5277 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
5278 return NULL;
5279 Py_DECREF(x);
5280
5281 delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */
5282 if (delta == NULL)
5283 return NULL;
Alexander Belopolsky1bcbaab2010-10-14 17:03:51 +00005284 x = create_timezone(delta, NULL);
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005285 Py_DECREF(delta);
5286 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
5287 return NULL;
5288 Py_DECREF(x);
5289
Alexander Belopolskya4415142012-06-08 12:33:09 -04005290 /* Epoch */
5291 PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0,
5292 PyDateTime_TimeZone_UTC);
5293 if (PyDateTime_Epoch == NULL)
5294 return NULL;
5295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 /* module initialization */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005297 PyModule_AddIntMacro(m, MINYEAR);
5298 PyModule_AddIntMacro(m, MAXYEAR);
Tim Peters2a799bf2002-12-16 20:18:38 +00005299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 Py_INCREF(&PyDateTime_DateType);
5301 PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType);
Tim Peters2a799bf2002-12-16 20:18:38 +00005302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 Py_INCREF(&PyDateTime_DateTimeType);
5304 PyModule_AddObject(m, "datetime",
5305 (PyObject *)&PyDateTime_DateTimeType);
Tim Petersa9bc1682003-01-11 03:39:11 +00005306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 Py_INCREF(&PyDateTime_TimeType);
5308 PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType);
Tim Petersa9bc1682003-01-11 03:39:11 +00005309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 Py_INCREF(&PyDateTime_DeltaType);
5311 PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType);
Tim Peters2a799bf2002-12-16 20:18:38 +00005312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 Py_INCREF(&PyDateTime_TZInfoType);
5314 PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType);
Tim Peters2a799bf2002-12-16 20:18:38 +00005315
Alexander Belopolsky4e749a12010-06-14 14:15:50 +00005316 Py_INCREF(&PyDateTime_TimeZoneType);
5317 PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType);
5318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL);
5320 if (x == NULL)
5321 return NULL;
5322 PyModule_AddObject(m, "datetime_CAPI", x);
Tim Peters9ddf40b2004-06-20 22:41:32 +00005323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 /* A 4-year cycle has an extra leap day over what we'd get from
5325 * pasting together 4 single years.
5326 */
5327 assert(DI4Y == 4 * 365 + 1);
5328 assert(DI4Y == days_before_year(4+1));
Tim Peters2a799bf2002-12-16 20:18:38 +00005329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 /* Similarly, a 400-year cycle has an extra leap day over what we'd
5331 * get from pasting together 4 100-year cycles.
5332 */
5333 assert(DI400Y == 4 * DI100Y + 1);
5334 assert(DI400Y == days_before_year(400+1));
Tim Peters2a799bf2002-12-16 20:18:38 +00005335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 /* OTOH, a 100-year cycle has one fewer leap day than we'd get from
5337 * pasting together 25 4-year cycles.
5338 */
5339 assert(DI100Y == 25 * DI4Y - 1);
5340 assert(DI100Y == days_before_year(100+1));
Tim Peters2a799bf2002-12-16 20:18:38 +00005341
Alexander Belopolsky790d2692013-08-04 14:51:35 -04005342 one = PyLong_FromLong(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 us_per_ms = PyLong_FromLong(1000);
5344 us_per_second = PyLong_FromLong(1000000);
5345 us_per_minute = PyLong_FromLong(60000000);
5346 seconds_per_day = PyLong_FromLong(24 * 3600);
Alexander Belopolsky790d2692013-08-04 14:51:35 -04005347 if (one == NULL || us_per_ms == NULL || us_per_second == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005348 us_per_minute == NULL || seconds_per_day == NULL)
5349 return NULL;
Tim Peters2a799bf2002-12-16 20:18:38 +00005350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 /* The rest are too big for 32-bit ints, but even
5352 * us_per_week fits in 40 bits, so doubles should be exact.
5353 */
5354 us_per_hour = PyLong_FromDouble(3600000000.0);
5355 us_per_day = PyLong_FromDouble(86400000000.0);
5356 us_per_week = PyLong_FromDouble(604800000000.0);
5357 if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
5358 return NULL;
5359 return m;
Tim Peters2a799bf2002-12-16 20:18:38 +00005360}
Tim Petersf3615152003-01-01 21:51:37 +00005361
5362/* ---------------------------------------------------------------------------
Tim Petersa9bc1682003-01-11 03:39:11 +00005363Some time zone algebra. For a datetime x, let
Tim Petersf3615152003-01-01 21:51:37 +00005364 x.n = x stripped of its timezone -- its naive time.
5365 x.o = x.utcoffset(), and assuming that doesn't raise an exception or
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 return None
Tim Petersf3615152003-01-01 21:51:37 +00005367 x.d = x.dst(), and assuming that doesn't raise an exception or
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 return None
Tim Petersf3615152003-01-01 21:51:37 +00005369 x.s = x's standard offset, x.o - x.d
5370
5371Now some derived rules, where k is a duration (timedelta).
5372
53731. x.o = x.s + x.d
5374 This follows from the definition of x.s.
5375
Tim Petersc5dc4da2003-01-02 17:55:03 +000053762. If x and y have the same tzinfo member, x.s = y.s.
Tim Petersf3615152003-01-01 21:51:37 +00005377 This is actually a requirement, an assumption we need to make about
5378 sane tzinfo classes.
5379
53803. The naive UTC time corresponding to x is x.n - x.o.
5381 This is again a requirement for a sane tzinfo class.
5382
53834. (x+k).s = x.s
Tim Peters8bb5ad22003-01-24 02:44:45 +00005384 This follows from #2, and that datimetimetz+timedelta preserves tzinfo.
Tim Petersf3615152003-01-01 21:51:37 +00005385
Tim Petersc5dc4da2003-01-02 17:55:03 +000053865. (x+k).n = x.n + k
Tim Petersf3615152003-01-01 21:51:37 +00005387 Again follows from how arithmetic is defined.
5388
Tim Peters8bb5ad22003-01-24 02:44:45 +00005389Now we can explain tz.fromutc(x). Let's assume it's an interesting case
Tim Petersf3615152003-01-01 21:51:37 +00005390(meaning that the various tzinfo methods exist, and don't blow up or return
5391None when called).
5392
Tim Petersa9bc1682003-01-11 03:39:11 +00005393The function wants to return a datetime y with timezone tz, equivalent to x.
Tim Peters8bb5ad22003-01-24 02:44:45 +00005394x is already in UTC.
Tim Petersf3615152003-01-01 21:51:37 +00005395
5396By #3, we want
5397
Tim Peters8bb5ad22003-01-24 02:44:45 +00005398 y.n - y.o = x.n [1]
Tim Petersf3615152003-01-01 21:51:37 +00005399
5400The algorithm starts by attaching tz to x.n, and calling that y. So
5401x.n = y.n at the start. Then it wants to add a duration k to y, so that [1]
5402becomes true; in effect, we want to solve [2] for k:
5403
Tim Peters8bb5ad22003-01-24 02:44:45 +00005404 (y+k).n - (y+k).o = x.n [2]
Tim Petersf3615152003-01-01 21:51:37 +00005405
5406By #1, this is the same as
5407
Tim Peters8bb5ad22003-01-24 02:44:45 +00005408 (y+k).n - ((y+k).s + (y+k).d) = x.n [3]
Tim Petersf3615152003-01-01 21:51:37 +00005409
5410By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start.
5411Substituting that into [3],
5412
Tim Peters8bb5ad22003-01-24 02:44:45 +00005413 x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving
5414 k - (y+k).s - (y+k).d = 0; rearranging,
5415 k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so
5416 k = y.s - (y+k).d
Tim Petersf3615152003-01-01 21:51:37 +00005417
Tim Peters8bb5ad22003-01-24 02:44:45 +00005418On the RHS, (y+k).d can't be computed directly, but y.s can be, and we
5419approximate k by ignoring the (y+k).d term at first. Note that k can't be
5420very large, since all offset-returning methods return a duration of magnitude
5421less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must
5422be 0, so ignoring it has no consequence then.
Tim Petersf3615152003-01-01 21:51:37 +00005423
5424In any case, the new value is
5425
Tim Peters8bb5ad22003-01-24 02:44:45 +00005426 z = y + y.s [4]
Tim Petersf3615152003-01-01 21:51:37 +00005427
Tim Peters8bb5ad22003-01-24 02:44:45 +00005428It's helpful to step back at look at [4] from a higher level: it's simply
5429mapping from UTC to tz's standard time.
Tim Petersc5dc4da2003-01-02 17:55:03 +00005430
5431At this point, if
5432
Tim Peters8bb5ad22003-01-24 02:44:45 +00005433 z.n - z.o = x.n [5]
Tim Petersc5dc4da2003-01-02 17:55:03 +00005434
5435we have an equivalent time, and are almost done. The insecurity here is
Tim Petersf3615152003-01-01 21:51:37 +00005436at the start of daylight time. Picture US Eastern for concreteness. The wall
5437time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good
Tim Peters8bb5ad22003-01-24 02:44:45 +00005438sense then. The docs ask that an Eastern tzinfo class consider such a time to
5439be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST
5440on the day DST starts. We want to return the 1:MM EST spelling because that's
Tim Petersf3615152003-01-01 21:51:37 +00005441the only spelling that makes sense on the local wall clock.
5442
Tim Petersc5dc4da2003-01-02 17:55:03 +00005443In fact, if [5] holds at this point, we do have the standard-time spelling,
5444but that takes a bit of proof. We first prove a stronger result. What's the
5445difference between the LHS and RHS of [5]? Let
Tim Petersf3615152003-01-01 21:51:37 +00005446
Tim Peters8bb5ad22003-01-24 02:44:45 +00005447 diff = x.n - (z.n - z.o) [6]
Tim Petersf3615152003-01-01 21:51:37 +00005448
Tim Petersc5dc4da2003-01-02 17:55:03 +00005449Now
5450 z.n = by [4]
Tim Peters8bb5ad22003-01-24 02:44:45 +00005451 (y + y.s).n = by #5
5452 y.n + y.s = since y.n = x.n
5453 x.n + y.s = since z and y are have the same tzinfo member,
5454 y.s = z.s by #2
5455 x.n + z.s
Tim Petersf3615152003-01-01 21:51:37 +00005456
Tim Petersc5dc4da2003-01-02 17:55:03 +00005457Plugging that back into [6] gives
Tim Petersf3615152003-01-01 21:51:37 +00005458
Tim Petersc5dc4da2003-01-02 17:55:03 +00005459 diff =
Tim Peters8bb5ad22003-01-24 02:44:45 +00005460 x.n - ((x.n + z.s) - z.o) = expanding
5461 x.n - x.n - z.s + z.o = cancelling
5462 - z.s + z.o = by #2
Tim Petersc5dc4da2003-01-02 17:55:03 +00005463 z.d
Tim Petersf3615152003-01-01 21:51:37 +00005464
Tim Petersc5dc4da2003-01-02 17:55:03 +00005465So diff = z.d.
Tim Petersf3615152003-01-01 21:51:37 +00005466
Tim Petersc5dc4da2003-01-02 17:55:03 +00005467If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time
Tim Peters8bb5ad22003-01-24 02:44:45 +00005468spelling we wanted in the endcase described above. We're done. Contrarily,
5469if z.d = 0, then we have a UTC equivalent, and are also done.
Tim Petersf3615152003-01-01 21:51:37 +00005470
Tim Petersc5dc4da2003-01-02 17:55:03 +00005471If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to
5472add to z (in effect, z is in tz's standard time, and we need to shift the
Tim Peters8bb5ad22003-01-24 02:44:45 +00005473local clock into tz's daylight time).
Tim Petersf3615152003-01-01 21:51:37 +00005474
Tim Petersc5dc4da2003-01-02 17:55:03 +00005475Let
Tim Petersf3615152003-01-01 21:51:37 +00005476
Tim Peters4fede1a2003-01-04 00:26:59 +00005477 z' = z + z.d = z + diff [7]
Tim Petersc3bb26a2003-01-02 03:14:59 +00005478
Tim Peters4fede1a2003-01-04 00:26:59 +00005479and we can again ask whether
Tim Petersc3bb26a2003-01-02 03:14:59 +00005480
Tim Peters8bb5ad22003-01-24 02:44:45 +00005481 z'.n - z'.o = x.n [8]
Tim Petersc3bb26a2003-01-02 03:14:59 +00005482
Tim Peters8bb5ad22003-01-24 02:44:45 +00005483If so, we're done. If not, the tzinfo class is insane, according to the
5484assumptions we've made. This also requires a bit of proof. As before, let's
5485compute the difference between the LHS and RHS of [8] (and skipping some of
5486the justifications for the kinds of substitutions we've done several times
5487already):
Tim Peters4fede1a2003-01-04 00:26:59 +00005488
Tim Peters8bb5ad22003-01-24 02:44:45 +00005489 diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7]
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 x.n - (z.n + diff - z'.o) = replacing diff via [6]
5491 x.n - (z.n + x.n - (z.n - z.o) - z'.o) =
5492 x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n
5493 - z.n + z.n - z.o + z'.o = cancel z.n
5494 - z.o + z'.o = #1 twice
5495 -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo
5496 z'.d - z.d
Tim Peters4fede1a2003-01-04 00:26:59 +00005497
5498So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal,
Tim Peters8bb5ad22003-01-24 02:44:45 +00005499we've found the UTC-equivalent so are done. In fact, we stop with [7] and
5500return z', not bothering to compute z'.d.
Tim Peters4fede1a2003-01-04 00:26:59 +00005501
Tim Peters8bb5ad22003-01-24 02:44:45 +00005502How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by
5503a dst() offset, and starting *from* a time already in DST (we know z.d != 0),
5504would have to change the result dst() returns: we start in DST, and moving
5505a little further into it takes us out of DST.
Tim Peters4fede1a2003-01-04 00:26:59 +00005506
Tim Peters8bb5ad22003-01-24 02:44:45 +00005507There isn't a sane case where this can happen. The closest it gets is at
5508the end of DST, where there's an hour in UTC with no spelling in a hybrid
5509tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During
5510that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM
5511UTC) because the docs insist on that, but 0:MM is taken as being in daylight
5512time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local
5513clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in
5514standard time. Since that's what the local clock *does*, we want to map both
5515UTC hours 5:MM and 6:MM to 1:MM Eastern. The result is ambiguous
Tim Peters4fede1a2003-01-04 00:26:59 +00005516in local time, but so it goes -- it's the way the local clock works.
5517
Tim Peters8bb5ad22003-01-24 02:44:45 +00005518When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0,
5519so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going.
5520z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8]
Tim Peters4fede1a2003-01-04 00:26:59 +00005521(correctly) concludes that z' is not UTC-equivalent to x.
5522
5523Because we know z.d said z was in daylight time (else [5] would have held and
5524we would have stopped then), and we know z.d != z'.d (else [8] would have held
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005525and we would have stopped then), and there are only 2 possible values dst() can
Tim Peters4fede1a2003-01-04 00:26:59 +00005526return in Eastern, it follows that z'.d must be 0 (which it is in the example,
5527but the reasoning doesn't depend on the example -- it depends on there being
5528two possible dst() outcomes, one zero and the other non-zero). Therefore
Tim Peters8bb5ad22003-01-24 02:44:45 +00005529z' must be in standard time, and is the spelling we want in this case.
5530
5531Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is
5532concerned (because it takes z' as being in standard time rather than the
5533daylight time we intend here), but returning it gives the real-life "local
5534clock repeats an hour" behavior when mapping the "unspellable" UTC hour into
5535tz.
5536
5537When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with
5538the 1:MM standard time spelling we want.
5539
5540So how can this break? One of the assumptions must be violated. Two
5541possibilities:
5542
55431) [2] effectively says that y.s is invariant across all y belong to a given
5544 time zone. This isn't true if, for political reasons or continental drift,
5545 a region decides to change its base offset from UTC.
5546
55472) There may be versions of "double daylight" time where the tail end of
5548 the analysis gives up a step too early. I haven't thought about that
5549 enough to say.
5550
5551In any case, it's clear that the default fromutc() is strong enough to handle
5552"almost all" time zones: so long as the standard offset is invariant, it
5553doesn't matter if daylight time transition points change from year to year, or
5554if daylight time is skipped in some years; it doesn't matter how large or
5555small dst() may get within its bounds; and it doesn't even matter if some
5556perverse time zone returns a negative dst()). So a breaking case must be
5557pretty bizarre, and a tzinfo subclass can override fromutc() if it is.
Tim Petersf3615152003-01-01 21:51:37 +00005558--------------------------------------------------------------------------- */