Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | /* C implementation for the date/time type documented at |
| 2 | * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage |
| 3 | */ |
| 4 | |
| 5 | #include "Python.h" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6 | #include "structmember.h" |
| 7 | |
| 8 | #include <time.h> |
| 9 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 10 | /* Differentiate between building the core module and building extension |
| 11 | * modules. |
| 12 | */ |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 13 | #ifndef Py_BUILD_CORE |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 14 | #define Py_BUILD_CORE |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 15 | #endif |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 16 | #include "datetime.h" |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 17 | #undef Py_BUILD_CORE |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 18 | |
| 19 | /* We require that C int be at least 32 bits, and use int virtually |
| 20 | * everywhere. In just a few cases we use a temp long, where a Python |
| 21 | * API returns a C long. In such cases, we have to ensure that the |
| 22 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 23 | */ |
| 24 | #if SIZEOF_INT < 4 |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 25 | # error "_datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #define MINYEAR 1 |
| 29 | #define MAXYEAR 9999 |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 30 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 31 | |
| 32 | /* Nine decimal digits is easy to communicate, and leaves enough room |
| 33 | * so that two delta days can be added w/o fear of overflowing a signed |
| 34 | * 32-bit int, and with plenty of room left over to absorb any possible |
| 35 | * carries from adding seconds. |
| 36 | */ |
| 37 | #define MAX_DELTA_DAYS 999999999 |
| 38 | |
| 39 | /* Rename the long macros in datetime.h to more reasonable short names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | #define GET_YEAR PyDateTime_GET_YEAR |
| 41 | #define GET_MONTH PyDateTime_GET_MONTH |
| 42 | #define GET_DAY PyDateTime_GET_DAY |
| 43 | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
| 44 | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
| 45 | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
| 46 | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 47 | |
| 48 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 50 | ((o)->data[1] = ((v) & 0x00ff))) |
| 51 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 52 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 53 | |
| 54 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 56 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 57 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 58 | #define DATE_SET_MICROSECOND(o, v) \ |
| 59 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 60 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 61 | ((o)->data[9] = ((v) & 0x0000ff))) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 62 | |
| 63 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 65 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 66 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 67 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
| 68 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 69 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 70 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 71 | #define TIME_SET_MICROSECOND(o, v) \ |
| 72 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 73 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 74 | ((o)->data[5] = ((v) & 0x0000ff))) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 75 | |
| 76 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 78 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 79 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 82 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 83 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 84 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 85 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 86 | * p->hastzinfo. |
| 87 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 88 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 89 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 90 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 91 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 92 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 93 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 94 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 96 | */ |
| 97 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 98 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 99 | /* Forward declarations. */ |
| 100 | static PyTypeObject PyDateTime_DateType; |
| 101 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 102 | static PyTypeObject PyDateTime_DeltaType; |
| 103 | static PyTypeObject PyDateTime_TimeType; |
| 104 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 105 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 106 | |
| 107 | /* --------------------------------------------------------------------------- |
| 108 | * Math utilities. |
| 109 | */ |
| 110 | |
| 111 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 112 | * iff k^i has sign bit set and k^j has sign bit set, |
| 113 | * iff (k^i)&(k^j) has sign bit set. |
| 114 | */ |
| 115 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 117 | |
| 118 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 119 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 120 | * the real point of this. C will probably truncate instead (C99 |
| 121 | * requires truncation; C89 left it implementation-defined). |
| 122 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 123 | * for all the uses made of it. This simplifies the code and makes |
| 124 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 125 | * overflow case). |
| 126 | */ |
| 127 | static int |
| 128 | divmod(int x, int y, int *r) |
| 129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | assert(y > 0); |
| 133 | quo = x / y; |
| 134 | *r = x - quo * y; |
| 135 | if (*r < 0) { |
| 136 | --quo; |
| 137 | *r += y; |
| 138 | } |
| 139 | assert(0 <= *r && *r < y); |
| 140 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Tim Peters | 5d644dd | 2003-01-02 16:32:54 +0000 | [diff] [blame] | 143 | /* Round a double to the nearest long. |x| must be small enough to fit |
| 144 | * in a C long; this is not checked. |
| 145 | */ |
| 146 | static long |
| 147 | round_to_long(double x) |
| 148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | if (x >= 0.0) |
| 150 | x = floor(x + 0.5); |
| 151 | else |
| 152 | x = ceil(x - 0.5); |
| 153 | return (long)x; |
Tim Peters | 5d644dd | 2003-01-02 16:32:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 156 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 157 | * are rounded to even. |
| 158 | */ |
| 159 | static PyObject * |
| 160 | divide_nearest(PyObject *m, PyObject *n) |
| 161 | { |
| 162 | PyObject *result; |
| 163 | PyObject *temp; |
| 164 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 165 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 166 | if (temp == NULL) |
| 167 | return NULL; |
| 168 | result = PyTuple_GET_ITEM(temp, 0); |
| 169 | Py_INCREF(result); |
| 170 | Py_DECREF(temp); |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 175 | /* --------------------------------------------------------------------------- |
| 176 | * General calendrical helper functions |
| 177 | */ |
| 178 | |
| 179 | /* For each month ordinal in 1..12, the number of days in that month, |
| 180 | * and the number of days before that month in the same year. These |
| 181 | * are correct for non-leap years only. |
| 182 | */ |
| 183 | static int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | 0, /* unused; this vector uses 1-based indexing */ |
| 185 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | static int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | 0, /* unused; this vector uses 1-based indexing */ |
| 190 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | /* year -> 1 if leap year, else 0. */ |
| 194 | static int |
| 195 | is_leap(int year) |
| 196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | /* Cast year to unsigned. The result is the same either way, but |
| 198 | * C can generate faster code for unsigned mod than for signed |
| 199 | * mod (especially for % 4 -- a good compiler should just grab |
| 200 | * the last 2 bits when the LHS is unsigned). |
| 201 | */ |
| 202 | const unsigned int ayear = (unsigned int)year; |
| 203 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* year, month -> number of days in that month in that year */ |
| 207 | static int |
| 208 | days_in_month(int year, int month) |
| 209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | assert(month >= 1); |
| 211 | assert(month <= 12); |
| 212 | if (month == 2 && is_leap(year)) |
| 213 | return 29; |
| 214 | else |
| 215 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* year, month -> number of days in year preceeding first day of month */ |
| 219 | static int |
| 220 | days_before_month(int year, int month) |
| 221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | assert(month >= 1); |
| 225 | assert(month <= 12); |
| 226 | days = _days_before_month[month]; |
| 227 | if (month > 2 && is_leap(year)) |
| 228 | ++days; |
| 229 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* year -> number of days before January 1st of year. Remember that we |
| 233 | * start with year 1, so days_before_year(1) == 0. |
| 234 | */ |
| 235 | static int |
| 236 | days_before_year(int year) |
| 237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | int y = year - 1; |
| 239 | /* This is incorrect if year <= 0; we really want the floor |
| 240 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 241 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 243 | assert (year >= 1); |
| 244 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 248 | * the correct values is asserted in the module init function. |
| 249 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 251 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 252 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 253 | |
| 254 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 255 | static void |
| 256 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 261 | * leap years repeats exactly every 400 years. The basic strategy is |
| 262 | * to find the closest 400-year boundary at or before ordinal, then |
| 263 | * work with the offset from that boundary to ordinal. Life is much |
| 264 | * clearer if we subtract 1 from ordinal first -- then the values |
| 265 | * of ordinal at 400-year boundaries are exactly those divisible |
| 266 | * by DI400Y: |
| 267 | * |
| 268 | * D M Y n n-1 |
| 269 | * -- --- ---- ---------- ---------------- |
| 270 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 271 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 272 | * ... |
| 273 | * 30 Dec 000 -1 -2 |
| 274 | * 31 Dec 000 0 -1 |
| 275 | * 1 Jan 001 1 0 400-year boundary |
| 276 | * 2 Jan 001 2 1 |
| 277 | * 3 Jan 001 3 2 |
| 278 | * ... |
| 279 | * 31 Dec 400 DI400Y DI400Y -1 |
| 280 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 281 | */ |
| 282 | assert(ordinal >= 1); |
| 283 | --ordinal; |
| 284 | n400 = ordinal / DI400Y; |
| 285 | n = ordinal % DI400Y; |
| 286 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 289 | * year, to the desired date. Now compute how many 100-year cycles |
| 290 | * precede n. |
| 291 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 292 | * 100-year cycles precede the desired day, which implies the |
| 293 | * desired day is December 31 at the end of a 400-year cycle. |
| 294 | */ |
| 295 | n100 = n / DI100Y; |
| 296 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | /* Now compute how many 4-year cycles precede it. */ |
| 299 | n4 = n / DI4Y; |
| 300 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | /* And now how many single years. Again n1 can be 4, and again |
| 303 | * meaning that the desired day is December 31 at the end of the |
| 304 | * 4-year cycle. |
| 305 | */ |
| 306 | n1 = n / 365; |
| 307 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | *year += n100 * 100 + n4 * 4 + n1; |
| 310 | if (n1 == 4 || n100 == 4) { |
| 311 | assert(n == 0); |
| 312 | *year -= 1; |
| 313 | *month = 12; |
| 314 | *day = 31; |
| 315 | return; |
| 316 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | /* Now the year is correct, and n is the offset from January 1. We |
| 319 | * find the month via an estimate that's either exact or one too |
| 320 | * large. |
| 321 | */ |
| 322 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 323 | assert(leapyear == is_leap(*year)); |
| 324 | *month = (n + 50) >> 5; |
| 325 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 326 | if (preceding > n) { |
| 327 | /* estimate is too large */ |
| 328 | *month -= 1; |
| 329 | preceding -= days_in_month(*year, *month); |
| 330 | } |
| 331 | n -= preceding; |
| 332 | assert(0 <= n); |
| 333 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 339 | static int |
| 340 | ymd_to_ord(int year, int month, int day) |
| 341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 346 | static int |
| 347 | weekday(int year, int month, int day) |
| 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 353 | * first calendar week containing a Thursday. |
| 354 | */ |
| 355 | static int |
| 356 | iso_week1_monday(int year) |
| 357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 359 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 360 | int first_weekday = (first_day + 6) % 7; |
| 361 | /* ordinal of closest Monday at or before 1/1 */ |
| 362 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 365 | week1_monday += 7; |
| 366 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* --------------------------------------------------------------------------- |
| 370 | * Range checkers. |
| 371 | */ |
| 372 | |
| 373 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 374 | * If not, raise OverflowError and return -1. |
| 375 | */ |
| 376 | static int |
| 377 | check_delta_day_range(int days) |
| 378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 380 | return 0; |
| 381 | PyErr_Format(PyExc_OverflowError, |
| 382 | "days=%d; must have magnitude <= %d", |
| 383 | days, MAX_DELTA_DAYS); |
| 384 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 388 | * aren't, raise ValueError and return -1. |
| 389 | */ |
| 390 | static int |
| 391 | check_date_args(int year, int month, int day) |
| 392 | { |
| 393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | if (year < MINYEAR || year > MAXYEAR) { |
| 395 | PyErr_SetString(PyExc_ValueError, |
| 396 | "year is out of range"); |
| 397 | return -1; |
| 398 | } |
| 399 | if (month < 1 || month > 12) { |
| 400 | PyErr_SetString(PyExc_ValueError, |
| 401 | "month must be in 1..12"); |
| 402 | return -1; |
| 403 | } |
| 404 | if (day < 1 || day > days_in_month(year, month)) { |
| 405 | PyErr_SetString(PyExc_ValueError, |
| 406 | "day is out of range for month"); |
| 407 | return -1; |
| 408 | } |
| 409 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 413 | * aren't, raise ValueError and return -1. |
| 414 | */ |
| 415 | static int |
| 416 | check_time_args(int h, int m, int s, int us) |
| 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | if (h < 0 || h > 23) { |
| 419 | PyErr_SetString(PyExc_ValueError, |
| 420 | "hour must be in 0..23"); |
| 421 | return -1; |
| 422 | } |
| 423 | if (m < 0 || m > 59) { |
| 424 | PyErr_SetString(PyExc_ValueError, |
| 425 | "minute must be in 0..59"); |
| 426 | return -1; |
| 427 | } |
| 428 | if (s < 0 || s > 59) { |
| 429 | PyErr_SetString(PyExc_ValueError, |
| 430 | "second must be in 0..59"); |
| 431 | return -1; |
| 432 | } |
| 433 | if (us < 0 || us > 999999) { |
| 434 | PyErr_SetString(PyExc_ValueError, |
| 435 | "microsecond must be in 0..999999"); |
| 436 | return -1; |
| 437 | } |
| 438 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* --------------------------------------------------------------------------- |
| 442 | * Normalization utilities. |
| 443 | */ |
| 444 | |
| 445 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 446 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 447 | * at least factor, enough of *lo is converted into "hi" units so that |
| 448 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 449 | * is impossible. |
| 450 | */ |
| 451 | static void |
| 452 | normalize_pair(int *hi, int *lo, int factor) |
| 453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | assert(factor > 0); |
| 455 | assert(lo != hi); |
| 456 | if (*lo < 0 || *lo >= factor) { |
| 457 | const int num_hi = divmod(*lo, factor, lo); |
| 458 | const int new_hi = *hi + num_hi; |
| 459 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 460 | *hi = new_hi; |
| 461 | } |
| 462 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | * 0 <= *s < 24*3600 |
| 467 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 468 | * The input values must be such that the internals don't overflow. |
| 469 | * The way this routine is used, we don't get close. |
| 470 | */ |
| 471 | static void |
| 472 | normalize_d_s_us(int *d, int *s, int *us) |
| 473 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | if (*us < 0 || *us >= 1000000) { |
| 475 | normalize_pair(s, us, 1000000); |
| 476 | /* |s| can't be bigger than about |
| 477 | * |original s| + |original us|/1000000 now. |
| 478 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | } |
| 481 | if (*s < 0 || *s >= 24*3600) { |
| 482 | normalize_pair(d, s, 24*3600); |
| 483 | /* |d| can't be bigger than about |
| 484 | * |original d| + |
| 485 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 486 | */ |
| 487 | } |
| 488 | assert(0 <= *s && *s < 24*3600); |
| 489 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | * 1 <= *m <= 12 |
| 494 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 495 | * The input values must be such that the internals don't overflow. |
| 496 | * The way this routine is used, we don't get close. |
| 497 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 498 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 499 | normalize_y_m_d(int *y, int *m, int *d) |
| 500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 502 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 503 | /* In actual use, m is always the month component extracted from a |
| 504 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | /* Now only day can be out of bounds (year may also be out of bounds |
| 510 | * for a datetime object, but we don't care about that here). |
| 511 | * If day is out of bounds, what to do is arguable, but at least the |
| 512 | * method here is principled and explainable. |
| 513 | */ |
| 514 | dim = days_in_month(*y, *m); |
| 515 | if (*d < 1 || *d > dim) { |
| 516 | /* Move day-1 days from the first of the month. First try to |
| 517 | * get off cheap if we're only one day out of range |
| 518 | * (adjustments for timezone alone can't be worse than that). |
| 519 | */ |
| 520 | if (*d == 0) { |
| 521 | --*m; |
| 522 | if (*m > 0) |
| 523 | *d = days_in_month(*y, *m); |
| 524 | else { |
| 525 | --*y; |
| 526 | *m = 12; |
| 527 | *d = 31; |
| 528 | } |
| 529 | } |
| 530 | else if (*d == dim + 1) { |
| 531 | /* move forward a day */ |
| 532 | ++*m; |
| 533 | *d = 1; |
| 534 | if (*m > 12) { |
| 535 | *m = 1; |
| 536 | ++*y; |
| 537 | } |
| 538 | } |
| 539 | else { |
| 540 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 541 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 542 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 543 | goto error; |
| 544 | } else { |
| 545 | ord_to_ymd(ordinal, y, m, d); |
| 546 | return 0; |
| 547 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | assert(*m > 0); |
| 551 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 552 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 553 | return 0; |
| 554 | error: |
| 555 | PyErr_SetString(PyExc_OverflowError, |
| 556 | "date value out of range"); |
| 557 | return -1; |
| 558 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 562 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 563 | * failure, where failure means the adjusted year is out of bounds. |
| 564 | */ |
| 565 | static int |
| 566 | normalize_date(int *year, int *month, int *day) |
| 567 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 568 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* Force all the datetime fields into range. The parameters are both |
| 572 | * inputs and outputs. Returns < 0 on error. |
| 573 | */ |
| 574 | static int |
| 575 | normalize_datetime(int *year, int *month, int *day, |
| 576 | int *hour, int *minute, int *second, |
| 577 | int *microsecond) |
| 578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | normalize_pair(second, microsecond, 1000000); |
| 580 | normalize_pair(minute, second, 60); |
| 581 | normalize_pair(hour, minute, 60); |
| 582 | normalize_pair(day, hour, 24); |
| 583 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 587 | * Basic object allocation: tp_alloc implementations. These allocate |
| 588 | * Python objects of the right size and type, and do the Python object- |
| 589 | * initialization bit. If there's not enough memory, they return NULL after |
| 590 | * setting MemoryError. All data members remain uninitialized trash. |
| 591 | * |
| 592 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 593 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 594 | * tp_basicsize for the time and datetime types is set to the size of the |
| 595 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 596 | * allocate enough space for a tzinfo member whether or not one is actually |
| 597 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 598 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 599 | * using) just happens today to effectively ignore the nitems argument |
| 600 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 601 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 602 | * be changed to force a 0 nitems argument unless the type being allocated |
| 603 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 604 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 605 | */ |
| 606 | |
| 607 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 608 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | self = (PyObject *) |
| 613 | PyObject_MALLOC(aware ? |
| 614 | sizeof(PyDateTime_Time) : |
| 615 | sizeof(_PyDateTime_BaseTime)); |
| 616 | if (self == NULL) |
| 617 | return (PyObject *)PyErr_NoMemory(); |
| 618 | PyObject_INIT(self, type); |
| 619 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 623 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | self = (PyObject *) |
| 628 | PyObject_MALLOC(aware ? |
| 629 | sizeof(PyDateTime_DateTime) : |
| 630 | sizeof(_PyDateTime_BaseDateTime)); |
| 631 | if (self == NULL) |
| 632 | return (PyObject *)PyErr_NoMemory(); |
| 633 | PyObject_INIT(self, type); |
| 634 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* --------------------------------------------------------------------------- |
| 638 | * Helpers for setting object fields. These work on pointers to the |
| 639 | * appropriate base class. |
| 640 | */ |
| 641 | |
| 642 | /* For date and datetime. */ |
| 643 | static void |
| 644 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 645 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | self->hashcode = -1; |
| 647 | SET_YEAR(self, y); |
| 648 | SET_MONTH(self, m); |
| 649 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* --------------------------------------------------------------------------- |
| 653 | * Create various objects, mostly without range checking. |
| 654 | */ |
| 655 | |
| 656 | /* Create a date instance with no range checking. */ |
| 657 | static PyObject * |
| 658 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 659 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 663 | if (self != NULL) |
| 664 | set_date_fields(self, year, month, day); |
| 665 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 670 | |
| 671 | /* Create a datetime instance with no range checking. */ |
| 672 | static PyObject * |
| 673 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | PyDateTime_DateTime *self; |
| 677 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 680 | if (self != NULL) { |
| 681 | self->hastzinfo = aware; |
| 682 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 683 | DATE_SET_HOUR(self, hour); |
| 684 | DATE_SET_MINUTE(self, minute); |
| 685 | DATE_SET_SECOND(self, second); |
| 686 | DATE_SET_MICROSECOND(self, usecond); |
| 687 | if (aware) { |
| 688 | Py_INCREF(tzinfo); |
| 689 | self->tzinfo = tzinfo; |
| 690 | } |
| 691 | } |
| 692 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \ |
| 696 | new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \ |
| 697 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 698 | |
| 699 | /* Create a time instance with no range checking. */ |
| 700 | static PyObject * |
| 701 | new_time_ex(int hour, int minute, int second, int usecond, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | PyDateTime_Time *self; |
| 705 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 708 | if (self != NULL) { |
| 709 | self->hastzinfo = aware; |
| 710 | self->hashcode = -1; |
| 711 | TIME_SET_HOUR(self, hour); |
| 712 | TIME_SET_MINUTE(self, minute); |
| 713 | TIME_SET_SECOND(self, second); |
| 714 | TIME_SET_MICROSECOND(self, usecond); |
| 715 | if (aware) { |
| 716 | Py_INCREF(tzinfo); |
| 717 | self->tzinfo = tzinfo; |
| 718 | } |
| 719 | } |
| 720 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | #define new_time(hh, mm, ss, us, tzinfo) \ |
| 724 | new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 725 | |
| 726 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 727 | * true. Passing false is a speed optimization, if you know for sure |
| 728 | * that seconds and microseconds are already in their proper ranges. In any |
| 729 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 730 | * of range). |
| 731 | */ |
| 732 | static PyObject * |
| 733 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | if (normalize) |
| 739 | normalize_d_s_us(&days, &seconds, µseconds); |
| 740 | assert(0 <= seconds && seconds < 24*3600); |
| 741 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | if (check_delta_day_range(days) < 0) |
| 744 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 747 | if (self != NULL) { |
| 748 | self->hashcode = -1; |
| 749 | SET_TD_DAYS(self, days); |
| 750 | SET_TD_SECONDS(self, seconds); |
| 751 | SET_TD_MICROSECONDS(self, microseconds); |
| 752 | } |
| 753 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | #define new_delta(d, s, us, normalize) \ |
| 757 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 758 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 759 | |
| 760 | typedef struct |
| 761 | { |
| 762 | PyObject_HEAD |
| 763 | PyObject *offset; |
| 764 | PyObject *name; |
| 765 | } PyDateTime_TimeZone; |
| 766 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 767 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 768 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 769 | /* The interned Epoch datetime instance */ |
| 770 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 771 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 772 | /* Create new timezone instance checking offset range. This |
| 773 | function does not check the name argument. Caller must assure |
| 774 | that offset is a timedelta instance and name is either NULL |
| 775 | or a unicode object. */ |
| 776 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 777 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 778 | { |
| 779 | PyDateTime_TimeZone *self; |
| 780 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 781 | |
| 782 | assert(offset != NULL); |
| 783 | assert(PyDelta_Check(offset)); |
| 784 | assert(name == NULL || PyUnicode_Check(name)); |
| 785 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 786 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 787 | if (self == NULL) { |
| 788 | return NULL; |
| 789 | } |
| 790 | Py_INCREF(offset); |
| 791 | self->offset = offset; |
| 792 | Py_XINCREF(name); |
| 793 | self->name = name; |
| 794 | return (PyObject *)self; |
| 795 | } |
| 796 | |
| 797 | static int delta_bool(PyDateTime_Delta *self); |
| 798 | |
| 799 | static PyObject * |
| 800 | new_timezone(PyObject *offset, PyObject *name) |
| 801 | { |
| 802 | assert(offset != NULL); |
| 803 | assert(PyDelta_Check(offset)); |
| 804 | assert(name == NULL || PyUnicode_Check(name)); |
| 805 | |
| 806 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 807 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 808 | return PyDateTime_TimeZone_UTC; |
| 809 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 810 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 811 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 812 | " representing a whole number of minutes," |
| 813 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 814 | return NULL; |
| 815 | } |
| 816 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 817 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 818 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 819 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 820 | " timedelta(hours=24)," |
| 821 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 822 | return NULL; |
| 823 | } |
| 824 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 825 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 828 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 829 | * tzinfo helpers. |
| 830 | */ |
| 831 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 832 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 833 | * raise TypeError and return -1. |
| 834 | */ |
| 835 | static int |
| 836 | check_tzinfo_subclass(PyObject *p) |
| 837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | if (p == Py_None || PyTZInfo_Check(p)) |
| 839 | return 0; |
| 840 | PyErr_Format(PyExc_TypeError, |
| 841 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 842 | "not type '%s'", |
| 843 | Py_TYPE(p)->tp_name); |
| 844 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 847 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 848 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 849 | * and the caller must not decref the result. |
| 850 | */ |
| 851 | static PyObject * |
| 852 | get_tzinfo_member(PyObject *self) |
| 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 857 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 858 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 859 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 864 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 865 | * be an instance of the tzinfo class. If the method returns None, this |
| 866 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 867 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 868 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 869 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 870 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 871 | static PyObject * |
| 872 | call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 873 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 874 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 877 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 879 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 880 | if (tzinfo == Py_None) |
| 881 | Py_RETURN_NONE; |
| 882 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 883 | if (offset == Py_None || offset == NULL) |
| 884 | return offset; |
| 885 | if (PyDelta_Check(offset)) { |
| 886 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 887 | Py_DECREF(offset); |
| 888 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 889 | " representing a whole number of minutes"); |
| 890 | return NULL; |
| 891 | } |
| 892 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 893 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 894 | Py_DECREF(offset); |
| 895 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 896 | " strictly between -timedelta(hours=24) and" |
| 897 | " timedelta(hours=24)."); |
| 898 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 902 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | PyErr_Format(PyExc_TypeError, |
| 904 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 905 | "timedelta, not '%.200s'", |
| 906 | name, Py_TYPE(offset)->tp_name); |
| 907 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 909 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 910 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 914 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 915 | * returns None, call_utcoffset returns 0 and sets *none to 1. If uctoffset() |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 916 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
| 917 | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
| 918 | * # of minutes), ValueError is raised and this returns -1. Else *none is |
| 919 | * set to 0 and the offset is returned (as int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 920 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 921 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 922 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 923 | { |
| 924 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 927 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 928 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 929 | * returns None, call_dst returns 0 and sets *none to 1. If dst() |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 930 | & doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 931 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 932 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
| 933 | * the offset is returned (as an int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 934 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 935 | static PyObject * |
| 936 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 937 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 938 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 941 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 942 | * an instance of the tzinfo class or None. If tzinfo isn't None, and |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 943 | * tzname() doesn't return None or a string, TypeError is raised and this |
Guido van Rossum | e3d1d41 | 2007-05-23 21:24:35 +0000 | [diff] [blame] | 944 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 945 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 946 | */ |
| 947 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 948 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 949 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 951 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | assert(tzinfo != NULL); |
| 954 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 955 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 958 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 959 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 960 | result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 961 | |
| 962 | if (result == NULL || result == Py_None) |
| 963 | return result; |
| 964 | |
| 965 | if (!PyUnicode_Check(result)) { |
| 966 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 967 | "return None or a string, not '%s'", |
| 968 | Py_TYPE(result)->tp_name); |
| 969 | Py_DECREF(result); |
| 970 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 972 | |
| 973 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 976 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 977 | * stuff |
| 978 | * ", tzinfo=" + repr(tzinfo) |
| 979 | * before the closing ")". |
| 980 | */ |
| 981 | static PyObject * |
| 982 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | assert(PyUnicode_Check(repr)); |
| 987 | assert(tzinfo); |
| 988 | if (tzinfo == Py_None) |
| 989 | return repr; |
| 990 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 991 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 992 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | Py_DECREF(repr); |
| 994 | if (temp == NULL) |
| 995 | return NULL; |
| 996 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 997 | Py_DECREF(temp); |
| 998 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* --------------------------------------------------------------------------- |
| 1002 | * String format helpers. |
| 1003 | */ |
| 1004 | |
| 1005 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1006 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | static const char *DayNames[] = { |
| 1009 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1010 | }; |
| 1011 | static const char *MonthNames[] = { |
| 1012 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1013 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1014 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1019 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1020 | GET_DAY(date), hours, minutes, seconds, |
| 1021 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1024 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1025 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1026 | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
| 1027 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1028 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1029 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1030 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1031 | * string of the form |
| 1032 | * sign HH sep MM |
| 1033 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1034 | * bogus, an appropriate exception is set and -1 is returned. |
| 1035 | */ |
| 1036 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1037 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1039 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1040 | PyObject *offset; |
| 1041 | int hours, minutes, seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1045 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1046 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1047 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1049 | if (offset == Py_None) { |
| 1050 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | *buf = '\0'; |
| 1052 | return 0; |
| 1053 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1054 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1055 | if (GET_TD_DAYS(offset) < 0) { |
| 1056 | PyObject *temp = offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | sign = '-'; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1058 | offset = delta_negative((PyDateTime_Delta *)offset); |
| 1059 | Py_DECREF(temp); |
| 1060 | if (offset == NULL) |
| 1061 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1063 | else { |
| 1064 | sign = '+'; |
| 1065 | } |
| 1066 | /* Offset is not negative here. */ |
| 1067 | seconds = GET_TD_SECONDS(offset); |
| 1068 | Py_DECREF(offset); |
| 1069 | minutes = divmod(seconds, 60, &seconds); |
| 1070 | hours = divmod(minutes, 60, &minutes); |
| 1071 | assert(seconds == 0); |
| 1072 | /* XXX ignore sub-minute data, curently not allowed. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1078 | static PyObject * |
| 1079 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1080 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | PyObject *temp; |
| 1082 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1083 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1084 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | if (Zreplacement == NULL) |
| 1087 | return NULL; |
| 1088 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1089 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | assert(tzinfoarg != NULL); |
| 1092 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1093 | if (temp == NULL) |
| 1094 | goto Error; |
| 1095 | if (temp == Py_None) { |
| 1096 | Py_DECREF(temp); |
| 1097 | return Zreplacement; |
| 1098 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | assert(PyUnicode_Check(temp)); |
| 1101 | /* Since the tzname is getting stuffed into the |
| 1102 | * format, we have to double any % signs so that |
| 1103 | * strftime doesn't treat them as format codes. |
| 1104 | */ |
| 1105 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1106 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | Py_DECREF(temp); |
| 1108 | if (Zreplacement == NULL) |
| 1109 | return NULL; |
| 1110 | if (!PyUnicode_Check(Zreplacement)) { |
| 1111 | PyErr_SetString(PyExc_TypeError, |
| 1112 | "tzname.replace() did not return a string"); |
| 1113 | goto Error; |
| 1114 | } |
| 1115 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1116 | |
| 1117 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | Py_DECREF(Zreplacement); |
| 1119 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1122 | static PyObject * |
| 1123 | make_freplacement(PyObject *object) |
| 1124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | char freplacement[64]; |
| 1126 | if (PyTime_Check(object)) |
| 1127 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1128 | else if (PyDateTime_Check(object)) |
| 1129 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1130 | else |
| 1131 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1136 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1137 | * so this imports the module and calls it. All the hair is due to |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1138 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1139 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1140 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1141 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1142 | */ |
| 1143 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1144 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1150 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1151 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | const char *pin; /* pointer to next char in input format */ |
| 1154 | Py_ssize_t flen; /* length of input format */ |
| 1155 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1158 | char *pnew; /* pointer to available byte in output format */ |
| 1159 | size_t totalnew; /* number bytes total in output format buffer, |
| 1160 | exclusive of trailing \0 */ |
| 1161 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1164 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | assert(object && format && timetuple); |
| 1167 | assert(PyUnicode_Check(format)); |
| 1168 | /* Convert the input format to a C string and size */ |
| 1169 | pin = _PyUnicode_AsStringAndSize(format, &flen); |
| 1170 | if (!pin) |
| 1171 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1174 | * a new format. Since computing the replacements for those codes |
| 1175 | * is expensive, don't unless they're actually used. |
| 1176 | */ |
| 1177 | if (flen > INT_MAX - 1) { |
| 1178 | PyErr_NoMemory(); |
| 1179 | goto Done; |
| 1180 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1183 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1184 | if (newfmt == NULL) goto Done; |
| 1185 | pnew = PyBytes_AsString(newfmt); |
| 1186 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | while ((ch = *pin++) != '\0') { |
| 1189 | if (ch != '%') { |
| 1190 | ptoappend = pin - 1; |
| 1191 | ntoappend = 1; |
| 1192 | } |
| 1193 | else if ((ch = *pin++) == '\0') { |
| 1194 | /* There's a lone trailing %; doesn't make sense. */ |
| 1195 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1196 | "ends with raw %"); |
| 1197 | goto Done; |
| 1198 | } |
| 1199 | /* A % has been seen and ch is the character after it. */ |
| 1200 | else if (ch == 'z') { |
| 1201 | if (zreplacement == NULL) { |
| 1202 | /* format utcoffset */ |
| 1203 | char buf[100]; |
| 1204 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1205 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1206 | if (zreplacement == NULL) goto Done; |
| 1207 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1208 | assert(tzinfoarg != NULL); |
| 1209 | if (format_utcoffset(buf, |
| 1210 | sizeof(buf), |
| 1211 | "", |
| 1212 | tzinfo, |
| 1213 | tzinfoarg) < 0) |
| 1214 | goto Done; |
| 1215 | Py_DECREF(zreplacement); |
| 1216 | zreplacement = |
| 1217 | PyBytes_FromStringAndSize(buf, |
| 1218 | strlen(buf)); |
| 1219 | if (zreplacement == NULL) |
| 1220 | goto Done; |
| 1221 | } |
| 1222 | } |
| 1223 | assert(zreplacement != NULL); |
| 1224 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1225 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1226 | } |
| 1227 | else if (ch == 'Z') { |
| 1228 | /* format tzname */ |
| 1229 | if (Zreplacement == NULL) { |
| 1230 | Zreplacement = make_Zreplacement(object, |
| 1231 | tzinfoarg); |
| 1232 | if (Zreplacement == NULL) |
| 1233 | goto Done; |
| 1234 | } |
| 1235 | assert(Zreplacement != NULL); |
| 1236 | assert(PyUnicode_Check(Zreplacement)); |
| 1237 | ptoappend = _PyUnicode_AsStringAndSize(Zreplacement, |
| 1238 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1239 | if (ptoappend == NULL) |
| 1240 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | } |
| 1242 | else if (ch == 'f') { |
| 1243 | /* format microseconds */ |
| 1244 | if (freplacement == NULL) { |
| 1245 | freplacement = make_freplacement(object); |
| 1246 | if (freplacement == NULL) |
| 1247 | goto Done; |
| 1248 | } |
| 1249 | assert(freplacement != NULL); |
| 1250 | assert(PyBytes_Check(freplacement)); |
| 1251 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1252 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1253 | } |
| 1254 | else { |
| 1255 | /* percent followed by neither z nor Z */ |
| 1256 | ptoappend = pin - 2; |
| 1257 | ntoappend = 2; |
| 1258 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | /* Append the ntoappend chars starting at ptoappend to |
| 1261 | * the new format. |
| 1262 | */ |
| 1263 | if (ntoappend == 0) |
| 1264 | continue; |
| 1265 | assert(ptoappend != NULL); |
| 1266 | assert(ntoappend > 0); |
| 1267 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1268 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | PyErr_NoMemory(); |
| 1270 | goto Done; |
| 1271 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1272 | totalnew <<= 1; |
| 1273 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1276 | } |
| 1277 | memcpy(pnew, ptoappend, ntoappend); |
| 1278 | pnew += ntoappend; |
| 1279 | usednew += ntoappend; |
| 1280 | assert(usednew <= totalnew); |
| 1281 | } /* end while() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1284 | goto Done; |
| 1285 | { |
| 1286 | PyObject *format; |
| 1287 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | if (time == NULL) |
| 1290 | goto Done; |
| 1291 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1292 | if (format != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1293 | _Py_IDENTIFIER(strftime); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1294 | |
| 1295 | result = _PyObject_CallMethodId(time, &PyId_strftime, "OO", |
| 1296 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | Py_DECREF(format); |
| 1298 | } |
| 1299 | Py_DECREF(time); |
| 1300 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1301 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | Py_XDECREF(freplacement); |
| 1303 | Py_XDECREF(zreplacement); |
| 1304 | Py_XDECREF(Zreplacement); |
| 1305 | Py_XDECREF(newfmt); |
| 1306 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1309 | /* --------------------------------------------------------------------------- |
| 1310 | * Wrap functions from the time module. These aren't directly available |
| 1311 | * from C. Perhaps they should be. |
| 1312 | */ |
| 1313 | |
| 1314 | /* Call time.time() and return its result (a Python float). */ |
| 1315 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1316 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | PyObject *result = NULL; |
| 1319 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1322 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1323 | |
| 1324 | result = _PyObject_CallMethodId(time, &PyId_time, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | Py_DECREF(time); |
| 1326 | } |
| 1327 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1331 | * computed from the y,m,d args. |
| 1332 | */ |
| 1333 | static PyObject * |
| 1334 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | PyObject *time; |
| 1337 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | time = PyImport_ImportModuleNoBlock("time"); |
| 1340 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1341 | _Py_IDENTIFIER(struct_time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1342 | |
| 1343 | result = _PyObject_CallMethodId(time, &PyId_struct_time, |
| 1344 | "((iiiiiiiii))", |
| 1345 | y, m, d, |
| 1346 | hh, mm, ss, |
| 1347 | weekday(y, m, d), |
| 1348 | days_before_month(y, m) + d, |
| 1349 | dstflag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | Py_DECREF(time); |
| 1351 | } |
| 1352 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | /* --------------------------------------------------------------------------- |
| 1356 | * Miscellaneous helpers. |
| 1357 | */ |
| 1358 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1359 | /* For various reasons, we need to use tp_richcompare instead of tp_reserved. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1360 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1361 | * This little helper turns that into a bool result for rich comparisons. |
| 1362 | */ |
| 1363 | static PyObject * |
| 1364 | diff_to_bool(int diff, int op) |
| 1365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | PyObject *result; |
| 1367 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | switch (op) { |
| 1370 | case Py_EQ: istrue = diff == 0; break; |
| 1371 | case Py_NE: istrue = diff != 0; break; |
| 1372 | case Py_LE: istrue = diff <= 0; break; |
| 1373 | case Py_GE: istrue = diff >= 0; break; |
| 1374 | case Py_LT: istrue = diff < 0; break; |
| 1375 | case Py_GT: istrue = diff > 0; break; |
| 1376 | default: |
| 1377 | assert(! "op unknown"); |
| 1378 | istrue = 0; /* To shut up compiler */ |
| 1379 | } |
| 1380 | result = istrue ? Py_True : Py_False; |
| 1381 | Py_INCREF(result); |
| 1382 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1385 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1386 | static PyObject * |
| 1387 | cmperror(PyObject *a, PyObject *b) |
| 1388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PyErr_Format(PyExc_TypeError, |
| 1390 | "can't compare %s to %s", |
| 1391 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1392 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1395 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1396 | * Cached Python objects; these are set by the module init function. |
| 1397 | */ |
| 1398 | |
| 1399 | /* Conversion factors. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | static PyObject *us_per_us = NULL; /* 1 */ |
| 1401 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1402 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1403 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
| 1404 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python long */ |
| 1405 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python long */ |
| 1406 | static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1407 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1408 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1409 | /* --------------------------------------------------------------------------- |
| 1410 | * Class implementations. |
| 1411 | */ |
| 1412 | |
| 1413 | /* |
| 1414 | * PyDateTime_Delta implementation. |
| 1415 | */ |
| 1416 | |
| 1417 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1419 | * as a Python int or long. |
| 1420 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1421 | * due to ubiquitous overflow possibilities. |
| 1422 | */ |
| 1423 | static PyObject * |
| 1424 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | PyObject *x1 = NULL; |
| 1427 | PyObject *x2 = NULL; |
| 1428 | PyObject *x3 = NULL; |
| 1429 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1431 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1432 | if (x1 == NULL) |
| 1433 | goto Done; |
| 1434 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1435 | if (x2 == NULL) |
| 1436 | goto Done; |
| 1437 | Py_DECREF(x1); |
| 1438 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | /* x2 has days in seconds */ |
| 1441 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1442 | if (x1 == NULL) |
| 1443 | goto Done; |
| 1444 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1445 | if (x3 == NULL) |
| 1446 | goto Done; |
| 1447 | Py_DECREF(x1); |
| 1448 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1449 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | /* x3 has days+seconds in seconds */ |
| 1452 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1453 | if (x1 == NULL) |
| 1454 | goto Done; |
| 1455 | Py_DECREF(x3); |
| 1456 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | /* x1 has days+seconds in us */ |
| 1459 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1460 | if (x2 == NULL) |
| 1461 | goto Done; |
| 1462 | result = PyNumber_Add(x1, x2); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1463 | |
| 1464 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | Py_XDECREF(x1); |
| 1466 | Py_XDECREF(x2); |
| 1467 | Py_XDECREF(x3); |
| 1468 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* Convert a number of us (as a Python int or long) to a timedelta. |
| 1472 | */ |
| 1473 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1474 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | int us; |
| 1477 | int s; |
| 1478 | int d; |
| 1479 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | PyObject *tuple = NULL; |
| 1482 | PyObject *num = NULL; |
| 1483 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1485 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1486 | if (tuple == NULL) |
| 1487 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1490 | if (num == NULL) |
| 1491 | goto Done; |
| 1492 | temp = PyLong_AsLong(num); |
| 1493 | num = NULL; |
| 1494 | if (temp == -1 && PyErr_Occurred()) |
| 1495 | goto Done; |
| 1496 | assert(0 <= temp && temp < 1000000); |
| 1497 | us = (int)temp; |
| 1498 | if (us < 0) { |
| 1499 | /* The divisor was positive, so this must be an error. */ |
| 1500 | assert(PyErr_Occurred()); |
| 1501 | goto Done; |
| 1502 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1505 | if (num == NULL) |
| 1506 | goto Done; |
| 1507 | Py_INCREF(num); |
| 1508 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1511 | if (tuple == NULL) |
| 1512 | goto Done; |
| 1513 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1516 | if (num == NULL) |
| 1517 | goto Done; |
| 1518 | temp = PyLong_AsLong(num); |
| 1519 | num = NULL; |
| 1520 | if (temp == -1 && PyErr_Occurred()) |
| 1521 | goto Done; |
| 1522 | assert(0 <= temp && temp < 24*3600); |
| 1523 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | if (s < 0) { |
| 1526 | /* The divisor was positive, so this must be an error. */ |
| 1527 | assert(PyErr_Occurred()); |
| 1528 | goto Done; |
| 1529 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1532 | if (num == NULL) |
| 1533 | goto Done; |
| 1534 | Py_INCREF(num); |
| 1535 | temp = PyLong_AsLong(num); |
| 1536 | if (temp == -1 && PyErr_Occurred()) |
| 1537 | goto Done; |
| 1538 | d = (int)temp; |
| 1539 | if ((long)d != temp) { |
| 1540 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1541 | "large to fit in a C int"); |
| 1542 | goto Done; |
| 1543 | } |
| 1544 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1545 | |
| 1546 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | Py_XDECREF(tuple); |
| 1548 | Py_XDECREF(num); |
| 1549 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | #define microseconds_to_delta(pymicros) \ |
| 1553 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1554 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1555 | static PyObject * |
| 1556 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | PyObject *pyus_in; |
| 1559 | PyObject *pyus_out; |
| 1560 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | pyus_in = delta_to_microseconds(delta); |
| 1563 | if (pyus_in == NULL) |
| 1564 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1567 | Py_DECREF(pyus_in); |
| 1568 | if (pyus_out == NULL) |
| 1569 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | result = microseconds_to_delta(pyus_out); |
| 1572 | Py_DECREF(pyus_out); |
| 1573 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1577 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
| 1578 | { |
| 1579 | PyObject *result = NULL; |
| 1580 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1581 | PyObject *ratio = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1582 | _Py_IDENTIFIER(as_integer_ratio); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1583 | |
| 1584 | pyus_in = delta_to_microseconds(delta); |
| 1585 | if (pyus_in == NULL) |
| 1586 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1587 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1588 | if (ratio == NULL) |
| 1589 | goto error; |
| 1590 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
| 1591 | Py_DECREF(pyus_in); |
| 1592 | pyus_in = NULL; |
| 1593 | if (temp == NULL) |
| 1594 | goto error; |
| 1595 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
| 1596 | Py_DECREF(temp); |
| 1597 | if (pyus_out == NULL) |
| 1598 | goto error; |
| 1599 | result = microseconds_to_delta(pyus_out); |
| 1600 | Py_DECREF(pyus_out); |
| 1601 | error: |
| 1602 | Py_XDECREF(pyus_in); |
| 1603 | Py_XDECREF(ratio); |
| 1604 | |
| 1605 | return result; |
| 1606 | } |
| 1607 | |
| 1608 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1609 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | PyObject *pyus_in; |
| 1612 | PyObject *pyus_out; |
| 1613 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | pyus_in = delta_to_microseconds(delta); |
| 1616 | if (pyus_in == NULL) |
| 1617 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1620 | Py_DECREF(pyus_in); |
| 1621 | if (pyus_out == NULL) |
| 1622 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | result = microseconds_to_delta(pyus_out); |
| 1625 | Py_DECREF(pyus_out); |
| 1626 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1630 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | PyObject *pyus_left; |
| 1633 | PyObject *pyus_right; |
| 1634 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | pyus_left = delta_to_microseconds(left); |
| 1637 | if (pyus_left == NULL) |
| 1638 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | pyus_right = delta_to_microseconds(right); |
| 1641 | if (pyus_right == NULL) { |
| 1642 | Py_DECREF(pyus_left); |
| 1643 | return NULL; |
| 1644 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1647 | Py_DECREF(pyus_left); |
| 1648 | Py_DECREF(pyus_right); |
| 1649 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | static PyObject * |
| 1653 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | PyObject *pyus_left; |
| 1656 | PyObject *pyus_right; |
| 1657 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | pyus_left = delta_to_microseconds(left); |
| 1660 | if (pyus_left == NULL) |
| 1661 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | pyus_right = delta_to_microseconds(right); |
| 1664 | if (pyus_right == NULL) { |
| 1665 | Py_DECREF(pyus_left); |
| 1666 | return NULL; |
| 1667 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1670 | Py_DECREF(pyus_left); |
| 1671 | Py_DECREF(pyus_right); |
| 1672 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1676 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
| 1677 | { |
| 1678 | PyObject *result = NULL; |
| 1679 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1680 | PyObject *ratio = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1681 | _Py_IDENTIFIER(as_integer_ratio); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1682 | |
| 1683 | pyus_in = delta_to_microseconds(delta); |
| 1684 | if (pyus_in == NULL) |
| 1685 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1686 | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1687 | 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 | |
| 1707 | static PyObject * |
| 1708 | truedivide_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 | |
| 1725 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1726 | delta_add(PyObject *left, PyObject *right) |
| 1727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | if (result == Py_NotImplemented) |
| 1743 | Py_INCREF(result); |
| 1744 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static PyObject * |
| 1748 | delta_negative(PyDateTime_Delta *self) |
| 1749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | return new_delta(-GET_TD_DAYS(self), |
| 1751 | -GET_TD_SECONDS(self), |
| 1752 | -GET_TD_MICROSECONDS(self), |
| 1753 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static PyObject * |
| 1757 | delta_positive(PyDateTime_Delta *self) |
| 1758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | /* 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | static PyObject * |
| 1769 | delta_abs(PyDateTime_Delta *self) |
| 1770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1774 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | if (GET_TD_DAYS(self) < 0) |
| 1777 | result = delta_negative(self); |
| 1778 | else |
| 1779 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static PyObject * |
| 1785 | delta_subtract(PyObject *left, PyObject *right) |
| 1786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1790 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 1791 | /* 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | if (result == Py_NotImplemented) |
| 1802 | Py_INCREF(result); |
| 1803 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1806 | static int |
| 1807 | delta_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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1819 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1820 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1823 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | return diff_to_bool(diff, op); |
| 1825 | } |
| 1826 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1827 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1832 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1833 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1834 | delta_hash(PyDateTime_Delta *self) |
| 1835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | static PyObject * |
| 1847 | delta_multiply(PyObject *left, PyObject *right) |
| 1848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | if (PyDelta_Check(left)) { |
| 1852 | /* delta * ??? */ |
| 1853 | if (PyLong_Check(right)) |
| 1854 | result = multiply_int_timedelta(right, |
| 1855 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1856 | else if (PyFloat_Check(right)) |
| 1857 | result = multiply_float_timedelta(right, |
| 1858 | (PyDateTime_Delta *) left); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | } |
| 1860 | else if (PyLong_Check(left)) |
| 1861 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1862 | (PyDateTime_Delta *) right); |
| 1863 | else if (PyFloat_Check(left)) |
| 1864 | result = multiply_float_timedelta(left, |
| 1865 | (PyDateTime_Delta *) right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | if (result == Py_NotImplemented) |
| 1868 | Py_INCREF(result); |
| 1869 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static PyObject * |
| 1873 | delta_divide(PyObject *left, PyObject *right) |
| 1874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | if (result == Py_NotImplemented) |
| 1890 | Py_INCREF(result); |
| 1891 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1894 | static PyObject * |
| 1895 | delta_truedivide(PyObject *left, PyObject *right) |
| 1896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | if (PyDelta_Check(left)) { |
| 1900 | if (PyDelta_Check(right)) |
| 1901 | result = truedivide_timedelta_timedelta( |
| 1902 | (PyDateTime_Delta *)left, |
| 1903 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1904 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | if (result == Py_NotImplemented) |
| 1913 | Py_INCREF(result); |
| 1914 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static PyObject * |
| 1918 | delta_remainder(PyObject *left, PyObject *right) |
| 1919 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | PyObject *pyus_left; |
| 1921 | PyObject *pyus_right; |
| 1922 | PyObject *pyus_remainder; |
| 1923 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1924 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1925 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1926 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1928 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1929 | if (pyus_left == NULL) |
| 1930 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1933 | if (pyus_right == NULL) { |
| 1934 | Py_DECREF(pyus_left); |
| 1935 | return NULL; |
| 1936 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | 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 Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | remainder = microseconds_to_delta(pyus_remainder); |
| 1945 | Py_DECREF(pyus_remainder); |
| 1946 | if (remainder == NULL) |
| 1947 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | static PyObject * |
| 1953 | delta_divmod(PyObject *left, PyObject *right) |
| 1954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | PyObject *pyus_left; |
| 1956 | PyObject *pyus_right; |
| 1957 | PyObject *divmod; |
| 1958 | PyObject *delta; |
| 1959 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1960 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1961 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1962 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1965 | if (pyus_left == NULL) |
| 1966 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1968 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1969 | if (pyus_right == NULL) { |
| 1970 | Py_DECREF(pyus_left); |
| 1971 | return NULL; |
| 1972 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | 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 Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | 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 Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1992 | /* 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 | */ |
| 2001 | static PyObject * |
| 2002 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2003 | double *leftover) |
| 2004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | PyObject *prod; |
| 2006 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | if (PyFloat_Check(num)) { |
| 2020 | double dnum; |
| 2021 | double fracpart; |
| 2022 | double intpart; |
| 2023 | PyObject *x; |
| 2024 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | /* 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | prod = PyNumber_Multiply(x, factor); |
| 2043 | Py_DECREF(x); |
| 2044 | if (prod == NULL) |
| 2045 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | sum = PyNumber_Add(sofar, prod); |
| 2048 | Py_DECREF(prod); |
| 2049 | if (sum == NULL) |
| 2050 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | y = PyNumber_Add(sum, x); |
| 2070 | Py_DECREF(sum); |
| 2071 | Py_DECREF(x); |
| 2072 | *leftover += fracpart; |
| 2073 | return y; |
| 2074 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | PyErr_Format(PyExc_TypeError, |
| 2077 | "unsupported type for timedelta %s component: %s", |
| 2078 | tag, Py_TYPE(num)->tp_name); |
| 2079 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | static PyObject * |
| 2083 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | /* 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2097 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2098 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | static char *keywords[] = { |
| 2101 | "days", "seconds", "microseconds", "milliseconds", |
| 2102 | "minutes", "hours", "weeks", NULL |
| 2103 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2106 | keywords, |
| 2107 | &day, &second, &us, |
| 2108 | &ms, &minute, &hour, &week) == 0) |
| 2109 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | x = PyLong_FromLong(0); |
| 2112 | if (x == NULL) |
| 2113 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | #define CLEANUP \ |
| 2116 | Py_DECREF(x); \ |
| 2117 | x = y; \ |
| 2118 | if (x == NULL) \ |
| 2119 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | if (us) { |
| 2122 | y = accum("microseconds", x, us, us_per_us, &leftover_us); |
| 2123 | 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. */ |
| 2151 | PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); |
| 2152 | if (temp == NULL) { |
| 2153 | Py_DECREF(x); |
| 2154 | goto Done; |
| 2155 | } |
| 2156 | y = PyNumber_Add(x, temp); |
| 2157 | Py_DECREF(temp); |
| 2158 | CLEANUP; |
| 2159 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | self = microseconds_to_delta_ex(x, type); |
| 2162 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2163 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2165 | |
| 2166 | #undef CLEANUP |
| 2167 | } |
| 2168 | |
| 2169 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2170 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | return (GET_TD_DAYS(self) != 0 |
| 2173 | || GET_TD_SECONDS(self) != 0 |
| 2174 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | static PyObject * |
| 2178 | delta_repr(PyDateTime_Delta *self) |
| 2179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2181 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2182 | Py_TYPE(self)->tp_name, |
| 2183 | GET_TD_DAYS(self), |
| 2184 | GET_TD_SECONDS(self), |
| 2185 | GET_TD_MICROSECONDS(self)); |
| 2186 | if (GET_TD_SECONDS(self) != 0) |
| 2187 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2188 | Py_TYPE(self)->tp_name, |
| 2189 | GET_TD_DAYS(self), |
| 2190 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | return PyUnicode_FromFormat("%s(%d)", |
| 2193 | Py_TYPE(self)->tp_name, |
| 2194 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | static PyObject * |
| 2198 | delta_str(PyDateTime_Delta *self) |
| 2199 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2200 | int us = GET_TD_MICROSECONDS(self); |
| 2201 | int seconds = GET_TD_SECONDS(self); |
| 2202 | int minutes = divmod(seconds, 60, &seconds); |
| 2203 | int hours = divmod(minutes, 60, &minutes); |
| 2204 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | if (days) { |
| 2207 | if (us) |
| 2208 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2209 | days, (days == 1 || days == -1) ? "" : "s", |
| 2210 | hours, minutes, seconds, us); |
| 2211 | else |
| 2212 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2213 | days, (days == 1 || days == -1) ? "" : "s", |
| 2214 | hours, minutes, seconds); |
| 2215 | } else { |
| 2216 | if (us) |
| 2217 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2218 | hours, minutes, seconds, us); |
| 2219 | else |
| 2220 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2221 | hours, minutes, seconds); |
| 2222 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2223 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2226 | /* Pickle support, a simple use of __reduce__. */ |
| 2227 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2228 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2229 | static PyObject * |
| 2230 | delta_getstate(PyDateTime_Delta *self) |
| 2231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2233 | GET_TD_SECONDS(self), |
| 2234 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2237 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2238 | delta_total_seconds(PyObject *self) |
| 2239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | PyObject *total_seconds; |
| 2241 | PyObject *total_microseconds; |
| 2242 | PyObject *one_million; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2245 | if (total_microseconds == NULL) |
| 2246 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | one_million = PyLong_FromLong(1000000L); |
| 2249 | if (one_million == NULL) { |
| 2250 | Py_DECREF(total_microseconds); |
| 2251 | return NULL; |
| 2252 | } |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | total_seconds = PyNumber_TrueDivide(total_microseconds, one_million); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | Py_DECREF(total_microseconds); |
| 2257 | Py_DECREF(one_million); |
| 2258 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2262 | delta_reduce(PyDateTime_Delta* self) |
| 2263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2268 | |
| 2269 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | {"days", T_INT, OFFSET(days), READONLY, |
| 2272 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2275 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2278 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2279 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2280 | }; |
| 2281 | |
| 2282 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2284 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2287 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2290 | }; |
| 2291 | |
| 2292 | static char delta_doc[] = |
| 2293 | PyDoc_STR("Difference between two datetime values."); |
| 2294 | |
| 2295 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | delta_add, /* nb_add */ |
| 2297 | delta_subtract, /* nb_subtract */ |
| 2298 | delta_multiply, /* nb_multiply */ |
| 2299 | delta_remainder, /* nb_remainder */ |
| 2300 | delta_divmod, /* nb_divmod */ |
| 2301 | 0, /* nb_power */ |
| 2302 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2303 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2304 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2305 | (inquiry)delta_bool, /* nb_bool */ |
| 2306 | 0, /*nb_invert*/ |
| 2307 | 0, /*nb_lshift*/ |
| 2308 | 0, /*nb_rshift*/ |
| 2309 | 0, /*nb_and*/ |
| 2310 | 0, /*nb_xor*/ |
| 2311 | 0, /*nb_or*/ |
| 2312 | 0, /*nb_int*/ |
| 2313 | 0, /*nb_reserved*/ |
| 2314 | 0, /*nb_float*/ |
| 2315 | 0, /*nb_inplace_add*/ |
| 2316 | 0, /*nb_inplace_subtract*/ |
| 2317 | 0, /*nb_inplace_multiply*/ |
| 2318 | 0, /*nb_inplace_remainder*/ |
| 2319 | 0, /*nb_inplace_power*/ |
| 2320 | 0, /*nb_inplace_lshift*/ |
| 2321 | 0, /*nb_inplace_rshift*/ |
| 2322 | 0, /*nb_inplace_and*/ |
| 2323 | 0, /*nb_inplace_xor*/ |
| 2324 | 0, /*nb_inplace_or*/ |
| 2325 | delta_divide, /* nb_floor_divide */ |
| 2326 | delta_truedivide, /* nb_true_divide */ |
| 2327 | 0, /* nb_inplace_floor_divide */ |
| 2328 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2329 | }; |
| 2330 | |
| 2331 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2333 | "datetime.timedelta", /* tp_name */ |
| 2334 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2335 | 0, /* tp_itemsize */ |
| 2336 | 0, /* tp_dealloc */ |
| 2337 | 0, /* tp_print */ |
| 2338 | 0, /* tp_getattr */ |
| 2339 | 0, /* tp_setattr */ |
| 2340 | 0, /* tp_reserved */ |
| 2341 | (reprfunc)delta_repr, /* tp_repr */ |
| 2342 | &delta_as_number, /* tp_as_number */ |
| 2343 | 0, /* tp_as_sequence */ |
| 2344 | 0, /* tp_as_mapping */ |
| 2345 | (hashfunc)delta_hash, /* tp_hash */ |
| 2346 | 0, /* tp_call */ |
| 2347 | (reprfunc)delta_str, /* tp_str */ |
| 2348 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2349 | 0, /* tp_setattro */ |
| 2350 | 0, /* tp_as_buffer */ |
| 2351 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2352 | delta_doc, /* tp_doc */ |
| 2353 | 0, /* tp_traverse */ |
| 2354 | 0, /* tp_clear */ |
| 2355 | delta_richcompare, /* tp_richcompare */ |
| 2356 | 0, /* tp_weaklistoffset */ |
| 2357 | 0, /* tp_iter */ |
| 2358 | 0, /* tp_iternext */ |
| 2359 | delta_methods, /* tp_methods */ |
| 2360 | delta_members, /* tp_members */ |
| 2361 | 0, /* tp_getset */ |
| 2362 | 0, /* tp_base */ |
| 2363 | 0, /* tp_dict */ |
| 2364 | 0, /* tp_descr_get */ |
| 2365 | 0, /* tp_descr_set */ |
| 2366 | 0, /* tp_dictoffset */ |
| 2367 | 0, /* tp_init */ |
| 2368 | 0, /* tp_alloc */ |
| 2369 | delta_new, /* tp_new */ |
| 2370 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2371 | }; |
| 2372 | |
| 2373 | /* |
| 2374 | * PyDateTime_Date implementation. |
| 2375 | */ |
| 2376 | |
| 2377 | /* Accessor properties. */ |
| 2378 | |
| 2379 | static PyObject * |
| 2380 | date_year(PyDateTime_Date *self, void *unused) |
| 2381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | static PyObject * |
| 2386 | date_month(PyDateTime_Date *self, void *unused) |
| 2387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | static PyObject * |
| 2392 | date_day(PyDateTime_Date *self, void *unused) |
| 2393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | {"year", (getter)date_year}, |
| 2399 | {"month", (getter)date_month}, |
| 2400 | {"day", (getter)date_day}, |
| 2401 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2402 | }; |
| 2403 | |
| 2404 | /* Constructors. */ |
| 2405 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2406 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2407 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2408 | static PyObject * |
| 2409 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | PyObject *self = NULL; |
| 2412 | PyObject *state; |
| 2413 | int year; |
| 2414 | int month; |
| 2415 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2417 | /* Check for invocation from pickle with __getstate__ state */ |
| 2418 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2419 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2420 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2421 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2422 | { |
| 2423 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2426 | if (me != NULL) { |
| 2427 | char *pdata = PyBytes_AS_STRING(state); |
| 2428 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2429 | me->hashcode = -1; |
| 2430 | } |
| 2431 | return (PyObject *)me; |
| 2432 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2434 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2435 | &year, &month, &day)) { |
| 2436 | if (check_date_args(year, month, day) < 0) |
| 2437 | return NULL; |
| 2438 | self = new_date_ex(year, month, day, type); |
| 2439 | } |
| 2440 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | /* Return new date from localtime(t). */ |
| 2444 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2445 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | struct tm *tm; |
| 2448 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2449 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2450 | if (_PyTime_ObjectToTime_t(obj, &t) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | tm = localtime(&t); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2454 | if (tm == NULL) { |
| 2455 | /* unconvertible time */ |
| 2456 | #ifdef EINVAL |
| 2457 | if (errno == 0) |
| 2458 | errno = EINVAL; |
| 2459 | #endif |
| 2460 | PyErr_SetFromErrno(PyExc_OSError); |
| 2461 | return NULL; |
| 2462 | } |
| 2463 | |
| 2464 | return PyObject_CallFunction(cls, "iii", |
| 2465 | tm->tm_year + 1900, |
| 2466 | tm->tm_mon + 1, |
| 2467 | tm->tm_mday); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /* Return new date from current time. |
| 2471 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2472 | * only way to be sure of that is to *call* time.time(). That's not |
| 2473 | * generally the same as calling C's time. |
| 2474 | */ |
| 2475 | static PyObject * |
| 2476 | date_today(PyObject *cls, PyObject *dummy) |
| 2477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | PyObject *time; |
| 2479 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2480 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | time = time_time(); |
| 2483 | if (time == NULL) |
| 2484 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | /* Note well: today() is a class method, so this may not call |
| 2487 | * date.fromtimestamp. For example, it may call |
| 2488 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2489 | * time.time() delivers; if someone were gonzo about optimization, |
| 2490 | * date.today() could get away with plain C time(). |
| 2491 | */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2492 | result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | Py_DECREF(time); |
| 2494 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2498 | static PyObject * |
| 2499 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2500 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2501 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2503 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2504 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2505 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2506 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2510 | * the ordinal is out of range. |
| 2511 | */ |
| 2512 | static PyObject * |
| 2513 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2514 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | PyObject *result = NULL; |
| 2516 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2518 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2519 | int year; |
| 2520 | int month; |
| 2521 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | if (ordinal < 1) |
| 2524 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2525 | ">= 1"); |
| 2526 | else { |
| 2527 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2528 | result = PyObject_CallFunction(cls, "iii", |
| 2529 | year, month, day); |
| 2530 | } |
| 2531 | } |
| 2532 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* |
| 2536 | * Date arithmetic. |
| 2537 | */ |
| 2538 | |
| 2539 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2540 | * instead. |
| 2541 | */ |
| 2542 | static PyObject * |
| 2543 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | PyObject *result = NULL; |
| 2546 | int year = GET_YEAR(date); |
| 2547 | int month = GET_MONTH(date); |
| 2548 | int deltadays = GET_TD_DAYS(delta); |
| 2549 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2550 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 | if (normalize_date(&year, &month, &day) >= 0) |
| 2553 | result = new_date(year, month, day); |
| 2554 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | static PyObject * |
| 2558 | date_add(PyObject *left, PyObject *right) |
| 2559 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2560 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2561 | Py_RETURN_NOTIMPLEMENTED; |
| 2562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | if (PyDate_Check(left)) { |
| 2564 | /* date + ??? */ |
| 2565 | if (PyDelta_Check(right)) |
| 2566 | /* date + delta */ |
| 2567 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2568 | (PyDateTime_Delta *) right, |
| 2569 | 0); |
| 2570 | } |
| 2571 | else { |
| 2572 | /* ??? + date |
| 2573 | * 'right' must be one of us, or we wouldn't have been called |
| 2574 | */ |
| 2575 | if (PyDelta_Check(left)) |
| 2576 | /* delta + date */ |
| 2577 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2578 | (PyDateTime_Delta *) left, |
| 2579 | 0); |
| 2580 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2581 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | static PyObject * |
| 2585 | date_subtract(PyObject *left, PyObject *right) |
| 2586 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2587 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2588 | Py_RETURN_NOTIMPLEMENTED; |
| 2589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | if (PyDate_Check(left)) { |
| 2591 | if (PyDate_Check(right)) { |
| 2592 | /* date - date */ |
| 2593 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2594 | GET_MONTH(left), |
| 2595 | GET_DAY(left)); |
| 2596 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2597 | GET_MONTH(right), |
| 2598 | GET_DAY(right)); |
| 2599 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2600 | } |
| 2601 | if (PyDelta_Check(right)) { |
| 2602 | /* date - delta */ |
| 2603 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2604 | (PyDateTime_Delta *) right, |
| 2605 | 1); |
| 2606 | } |
| 2607 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2608 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | |
| 2612 | /* Various ways to turn a date into a string. */ |
| 2613 | |
| 2614 | static PyObject * |
| 2615 | date_repr(PyDateTime_Date *self) |
| 2616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2618 | Py_TYPE(self)->tp_name, |
| 2619 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | static PyObject * |
| 2623 | date_isoformat(PyDateTime_Date *self) |
| 2624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2626 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2629 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2630 | static PyObject * |
| 2631 | date_str(PyDateTime_Date *self) |
| 2632 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2633 | _Py_IDENTIFIER(isoformat); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2634 | |
| 2635 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | |
| 2639 | static PyObject * |
| 2640 | date_ctime(PyDateTime_Date *self) |
| 2641 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | static PyObject * |
| 2646 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | /* This method can be inherited, and needs to call the |
| 2649 | * timetuple() method appropriate to self's class. |
| 2650 | */ |
| 2651 | PyObject *result; |
| 2652 | PyObject *tuple; |
| 2653 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2654 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2658 | &format)) |
| 2659 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2660 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2661 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | if (tuple == NULL) |
| 2663 | return NULL; |
| 2664 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2665 | (PyObject *)self); |
| 2666 | Py_DECREF(tuple); |
| 2667 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2670 | static PyObject * |
| 2671 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2674 | _Py_IDENTIFIER(strftime); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2677 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2680 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2682 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2683 | return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2686 | /* ISO methods. */ |
| 2687 | |
| 2688 | static PyObject * |
| 2689 | date_isoweekday(PyDateTime_Date *self) |
| 2690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | static PyObject * |
| 2697 | date_isocalendar(PyDateTime_Date *self) |
| 2698 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | int year = GET_YEAR(self); |
| 2700 | int week1_monday = iso_week1_monday(year); |
| 2701 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2702 | int week; |
| 2703 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | week = divmod(today - week1_monday, 7, &day); |
| 2706 | if (week < 0) { |
| 2707 | --year; |
| 2708 | week1_monday = iso_week1_monday(year); |
| 2709 | week = divmod(today - week1_monday, 7, &day); |
| 2710 | } |
| 2711 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2712 | ++year; |
| 2713 | week = 0; |
| 2714 | } |
| 2715 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /* Miscellaneous methods. */ |
| 2719 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2720 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2721 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | if (PyDate_Check(other)) { |
| 2724 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2725 | ((PyDateTime_Date *)other)->data, |
| 2726 | _PyDateTime_DATE_DATASIZE); |
| 2727 | return diff_to_bool(diff, op); |
| 2728 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2729 | else |
| 2730 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | static PyObject * |
| 2734 | date_timetuple(PyDateTime_Date *self) |
| 2735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | return build_struct_time(GET_YEAR(self), |
| 2737 | GET_MONTH(self), |
| 2738 | GET_DAY(self), |
| 2739 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2742 | static PyObject * |
| 2743 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | PyObject *clone; |
| 2746 | PyObject *tuple; |
| 2747 | int year = GET_YEAR(self); |
| 2748 | int month = GET_MONTH(self); |
| 2749 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2752 | &year, &month, &day)) |
| 2753 | return NULL; |
| 2754 | tuple = Py_BuildValue("iii", year, month, day); |
| 2755 | if (tuple == NULL) |
| 2756 | return NULL; |
| 2757 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2758 | Py_DECREF(tuple); |
| 2759 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2762 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2763 | generic_hash(unsigned char *data, int len) |
| 2764 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2765 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | |
| 2769 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2770 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2771 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2772 | date_hash(PyDateTime_Date *self) |
| 2773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | if (self->hashcode == -1) |
| 2775 | self->hashcode = generic_hash( |
| 2776 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | static PyObject * |
| 2782 | date_toordinal(PyDateTime_Date *self) |
| 2783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2785 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | static PyObject * |
| 2789 | date_weekday(PyDateTime_Date *self) |
| 2790 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2796 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2797 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2798 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2799 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2800 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | PyObject* field; |
| 2803 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2804 | _PyDateTime_DATE_DATASIZE); |
| 2805 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2809 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2819 | METH_CLASS, |
| 2820 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2821 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2824 | METH_CLASS, |
| 2825 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2826 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2829 | PyDoc_STR("Current date or datetime: same as " |
| 2830 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2835 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2838 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2841 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2844 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2847 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2848 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2851 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2854 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2855 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2858 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2859 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2862 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2863 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2866 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2869 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2872 | }; |
| 2873 | |
| 2874 | static char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2875 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2876 | |
| 2877 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | date_add, /* nb_add */ |
| 2879 | date_subtract, /* nb_subtract */ |
| 2880 | 0, /* nb_multiply */ |
| 2881 | 0, /* nb_remainder */ |
| 2882 | 0, /* nb_divmod */ |
| 2883 | 0, /* nb_power */ |
| 2884 | 0, /* nb_negative */ |
| 2885 | 0, /* nb_positive */ |
| 2886 | 0, /* nb_absolute */ |
| 2887 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2888 | }; |
| 2889 | |
| 2890 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2892 | "datetime.date", /* tp_name */ |
| 2893 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2894 | 0, /* tp_itemsize */ |
| 2895 | 0, /* tp_dealloc */ |
| 2896 | 0, /* tp_print */ |
| 2897 | 0, /* tp_getattr */ |
| 2898 | 0, /* tp_setattr */ |
| 2899 | 0, /* tp_reserved */ |
| 2900 | (reprfunc)date_repr, /* tp_repr */ |
| 2901 | &date_as_number, /* tp_as_number */ |
| 2902 | 0, /* tp_as_sequence */ |
| 2903 | 0, /* tp_as_mapping */ |
| 2904 | (hashfunc)date_hash, /* tp_hash */ |
| 2905 | 0, /* tp_call */ |
| 2906 | (reprfunc)date_str, /* tp_str */ |
| 2907 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2908 | 0, /* tp_setattro */ |
| 2909 | 0, /* tp_as_buffer */ |
| 2910 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2911 | date_doc, /* tp_doc */ |
| 2912 | 0, /* tp_traverse */ |
| 2913 | 0, /* tp_clear */ |
| 2914 | date_richcompare, /* tp_richcompare */ |
| 2915 | 0, /* tp_weaklistoffset */ |
| 2916 | 0, /* tp_iter */ |
| 2917 | 0, /* tp_iternext */ |
| 2918 | date_methods, /* tp_methods */ |
| 2919 | 0, /* tp_members */ |
| 2920 | date_getset, /* tp_getset */ |
| 2921 | 0, /* tp_base */ |
| 2922 | 0, /* tp_dict */ |
| 2923 | 0, /* tp_descr_get */ |
| 2924 | 0, /* tp_descr_set */ |
| 2925 | 0, /* tp_dictoffset */ |
| 2926 | 0, /* tp_init */ |
| 2927 | 0, /* tp_alloc */ |
| 2928 | date_new, /* tp_new */ |
| 2929 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2930 | }; |
| 2931 | |
| 2932 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2933 | * PyDateTime_TZInfo implementation. |
| 2934 | */ |
| 2935 | |
| 2936 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 2937 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 2938 | * to derive from this. This is mostly for clarity, and for efficiency in |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 2939 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2940 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 2941 | * |
| 2942 | * Note: For reasons having to do with pickling of subclasses, we have |
| 2943 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 2944 | * in the Python implementation (__init__() could raise NotImplementedError |
| 2945 | * there without ill effect), but doing so in the C implementation hit a |
| 2946 | * brick wall. |
| 2947 | */ |
| 2948 | |
| 2949 | static PyObject * |
| 2950 | tzinfo_nogo(const char* methodname) |
| 2951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | PyErr_Format(PyExc_NotImplementedError, |
| 2953 | "a tzinfo subclass must implement %s()", |
| 2954 | methodname); |
| 2955 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | /* Methods. A subclass must implement these. */ |
| 2959 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2960 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2961 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 2962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2966 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2967 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 2968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2972 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2973 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 2974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2978 | |
| 2979 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 2980 | PyDateTime_Delta *delta, |
| 2981 | int factor); |
| 2982 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 2983 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 2984 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2985 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2986 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2987 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2988 | PyObject *result = NULL; |
| 2989 | PyObject *off = NULL, *dst = NULL; |
| 2990 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2991 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2992 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | PyErr_SetString(PyExc_TypeError, |
| 2994 | "fromutc: argument must be a datetime"); |
| 2995 | return NULL; |
| 2996 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2997 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 2999 | "is not self"); |
| 3000 | return NULL; |
| 3001 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3002 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3003 | off = datetime_utcoffset(dt, NULL); |
| 3004 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3006 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3008 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3009 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3011 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3012 | dst = datetime_dst(dt, NULL); |
| 3013 | if (dst == NULL) |
| 3014 | goto Fail; |
| 3015 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3017 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3018 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3020 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3021 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3022 | if (delta == NULL) |
| 3023 | goto Fail; |
| 3024 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3027 | |
| 3028 | Py_DECREF(dst); |
| 3029 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3030 | if (dst == NULL) |
| 3031 | goto Fail; |
| 3032 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | goto Inconsistent; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3034 | if (delta_bool(delta) != 0) { |
| 3035 | PyObject *temp = result; |
| 3036 | result = add_datetime_timedelta((PyDateTime_DateTime *)result, |
| 3037 | (PyDateTime_Delta *)dst, 1); |
| 3038 | Py_DECREF(temp); |
| 3039 | if (result == NULL) |
| 3040 | goto Fail; |
| 3041 | } |
| 3042 | Py_DECREF(delta); |
| 3043 | Py_DECREF(dst); |
| 3044 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3046 | |
| 3047 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3049 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3052 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3053 | Py_XDECREF(off); |
| 3054 | Py_XDECREF(dst); |
| 3055 | Py_XDECREF(delta); |
| 3056 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3057 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3060 | /* |
| 3061 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3062 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3063 | */ |
| 3064 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3065 | static PyObject * |
| 3066 | tzinfo_reduce(PyObject *self) |
| 3067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | PyObject *args, *state, *tmp; |
| 3069 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3070 | _Py_IDENTIFIER(__getinitargs__); |
| 3071 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | tmp = PyTuple_New(0); |
| 3074 | if (tmp == NULL) |
| 3075 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3076 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3077 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | if (getinitargs != NULL) { |
| 3079 | args = PyObject_CallObject(getinitargs, tmp); |
| 3080 | Py_DECREF(getinitargs); |
| 3081 | if (args == NULL) { |
| 3082 | Py_DECREF(tmp); |
| 3083 | return NULL; |
| 3084 | } |
| 3085 | } |
| 3086 | else { |
| 3087 | PyErr_Clear(); |
| 3088 | args = tmp; |
| 3089 | Py_INCREF(args); |
| 3090 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3091 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3092 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | if (getstate != NULL) { |
| 3094 | state = PyObject_CallObject(getstate, tmp); |
| 3095 | Py_DECREF(getstate); |
| 3096 | if (state == NULL) { |
| 3097 | Py_DECREF(args); |
| 3098 | Py_DECREF(tmp); |
| 3099 | return NULL; |
| 3100 | } |
| 3101 | } |
| 3102 | else { |
| 3103 | PyObject **dictptr; |
| 3104 | PyErr_Clear(); |
| 3105 | state = Py_None; |
| 3106 | dictptr = _PyObject_GetDictPtr(self); |
| 3107 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
| 3108 | state = *dictptr; |
| 3109 | Py_INCREF(state); |
| 3110 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | Py_DECREF(tmp); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | if (state == Py_None) { |
| 3115 | Py_DECREF(state); |
| 3116 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3117 | } |
| 3118 | else |
| 3119 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3120 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3121 | |
| 3122 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3125 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3128 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3129 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3132 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3135 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3138 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3141 | }; |
| 3142 | |
| 3143 | static char tzinfo_doc[] = |
| 3144 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3145 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3146 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3148 | "datetime.tzinfo", /* tp_name */ |
| 3149 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3150 | 0, /* tp_itemsize */ |
| 3151 | 0, /* tp_dealloc */ |
| 3152 | 0, /* tp_print */ |
| 3153 | 0, /* tp_getattr */ |
| 3154 | 0, /* tp_setattr */ |
| 3155 | 0, /* tp_reserved */ |
| 3156 | 0, /* tp_repr */ |
| 3157 | 0, /* tp_as_number */ |
| 3158 | 0, /* tp_as_sequence */ |
| 3159 | 0, /* tp_as_mapping */ |
| 3160 | 0, /* tp_hash */ |
| 3161 | 0, /* tp_call */ |
| 3162 | 0, /* tp_str */ |
| 3163 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3164 | 0, /* tp_setattro */ |
| 3165 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3166 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3167 | tzinfo_doc, /* tp_doc */ |
| 3168 | 0, /* tp_traverse */ |
| 3169 | 0, /* tp_clear */ |
| 3170 | 0, /* tp_richcompare */ |
| 3171 | 0, /* tp_weaklistoffset */ |
| 3172 | 0, /* tp_iter */ |
| 3173 | 0, /* tp_iternext */ |
| 3174 | tzinfo_methods, /* tp_methods */ |
| 3175 | 0, /* tp_members */ |
| 3176 | 0, /* tp_getset */ |
| 3177 | 0, /* tp_base */ |
| 3178 | 0, /* tp_dict */ |
| 3179 | 0, /* tp_descr_get */ |
| 3180 | 0, /* tp_descr_set */ |
| 3181 | 0, /* tp_dictoffset */ |
| 3182 | 0, /* tp_init */ |
| 3183 | 0, /* tp_alloc */ |
| 3184 | PyType_GenericNew, /* tp_new */ |
| 3185 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3186 | }; |
| 3187 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3188 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3189 | |
| 3190 | static PyObject * |
| 3191 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3192 | { |
| 3193 | PyObject *offset; |
| 3194 | PyObject *name = NULL; |
| 3195 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|O!:timezone", timezone_kws, |
| 3196 | &PyDateTime_DeltaType, &offset, |
| 3197 | &PyUnicode_Type, &name)) |
| 3198 | return new_timezone(offset, name); |
| 3199 | |
| 3200 | return NULL; |
| 3201 | } |
| 3202 | |
| 3203 | static void |
| 3204 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3205 | { |
| 3206 | Py_CLEAR(self->offset); |
| 3207 | Py_CLEAR(self->name); |
| 3208 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3209 | } |
| 3210 | |
| 3211 | static PyObject * |
| 3212 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3213 | PyDateTime_TimeZone *other, int op) |
| 3214 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3215 | if (op != Py_EQ && op != Py_NE) |
| 3216 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3217 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
| 3218 | if (op == Py_EQ) |
| 3219 | Py_RETURN_FALSE; |
| 3220 | else |
| 3221 | Py_RETURN_TRUE; |
| 3222 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3223 | return delta_richcompare(self->offset, other->offset, op); |
| 3224 | } |
| 3225 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3226 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3227 | timezone_hash(PyDateTime_TimeZone *self) |
| 3228 | { |
| 3229 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3230 | } |
| 3231 | |
| 3232 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3233 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3234 | otherwise. |
| 3235 | */ |
| 3236 | static int |
| 3237 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3238 | { |
| 3239 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3240 | return 0; |
| 3241 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3242 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3243 | return -1; |
| 3244 | } |
| 3245 | |
| 3246 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3247 | timezone_repr(PyDateTime_TimeZone *self) |
| 3248 | { |
| 3249 | /* Note that although timezone is not subclassable, it is convenient |
| 3250 | to use Py_TYPE(self)->tp_name here. */ |
| 3251 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3252 | |
| 3253 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3254 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3255 | |
| 3256 | if (self->name == NULL) |
| 3257 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3258 | |
| 3259 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3260 | self->name); |
| 3261 | } |
| 3262 | |
| 3263 | |
| 3264 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3265 | timezone_str(PyDateTime_TimeZone *self) |
| 3266 | { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3267 | int hours, minutes, seconds; |
| 3268 | PyObject *offset; |
| 3269 | char sign; |
| 3270 | |
| 3271 | if (self->name != NULL) { |
| 3272 | Py_INCREF(self->name); |
| 3273 | return self->name; |
| 3274 | } |
| 3275 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3276 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3277 | sign = '-'; |
| 3278 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3279 | if (offset == NULL) |
| 3280 | return NULL; |
| 3281 | } |
| 3282 | else { |
| 3283 | sign = '+'; |
| 3284 | offset = self->offset; |
| 3285 | Py_INCREF(offset); |
| 3286 | } |
| 3287 | /* Offset is not negative here. */ |
| 3288 | seconds = GET_TD_SECONDS(offset); |
| 3289 | Py_DECREF(offset); |
| 3290 | minutes = divmod(seconds, 60, &seconds); |
| 3291 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3292 | /* XXX ignore sub-minute data, curently not allowed. */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3293 | assert(seconds == 0); |
| 3294 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | static PyObject * |
| 3298 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3299 | { |
| 3300 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3301 | return NULL; |
| 3302 | |
| 3303 | return timezone_str(self); |
| 3304 | } |
| 3305 | |
| 3306 | static PyObject * |
| 3307 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3308 | { |
| 3309 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3310 | return NULL; |
| 3311 | |
| 3312 | Py_INCREF(self->offset); |
| 3313 | return self->offset; |
| 3314 | } |
| 3315 | |
| 3316 | static PyObject * |
| 3317 | timezone_dst(PyObject *self, PyObject *dt) |
| 3318 | { |
| 3319 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3320 | return NULL; |
| 3321 | |
| 3322 | Py_RETURN_NONE; |
| 3323 | } |
| 3324 | |
| 3325 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3326 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3327 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3328 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3329 | PyErr_SetString(PyExc_TypeError, |
| 3330 | "fromutc: argument must be a datetime"); |
| 3331 | return NULL; |
| 3332 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3333 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3334 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3335 | "is not self"); |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | |
| 3339 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3340 | } |
| 3341 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3342 | static PyObject * |
| 3343 | timezone_getinitargs(PyDateTime_TimeZone *self) |
| 3344 | { |
| 3345 | if (self->name == NULL) |
| 3346 | return Py_BuildValue("(O)", self->offset); |
| 3347 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3348 | } |
| 3349 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3350 | static PyMethodDef timezone_methods[] = { |
| 3351 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3352 | PyDoc_STR("If name is specified when timezone is created, returns the name." |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3353 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3354 | |
| 3355 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3356 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3357 | |
| 3358 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3359 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3360 | |
| 3361 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3362 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3363 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3364 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3365 | PyDoc_STR("pickle support")}, |
| 3366 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3367 | {NULL, NULL} |
| 3368 | }; |
| 3369 | |
| 3370 | static char timezone_doc[] = |
| 3371 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3372 | |
| 3373 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3374 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3375 | "datetime.timezone", /* tp_name */ |
| 3376 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3377 | 0, /* tp_itemsize */ |
| 3378 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3379 | 0, /* tp_print */ |
| 3380 | 0, /* tp_getattr */ |
| 3381 | 0, /* tp_setattr */ |
| 3382 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3383 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3384 | 0, /* tp_as_number */ |
| 3385 | 0, /* tp_as_sequence */ |
| 3386 | 0, /* tp_as_mapping */ |
| 3387 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3388 | 0, /* tp_call */ |
| 3389 | (reprfunc)timezone_str, /* tp_str */ |
| 3390 | 0, /* tp_getattro */ |
| 3391 | 0, /* tp_setattro */ |
| 3392 | 0, /* tp_as_buffer */ |
| 3393 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3394 | timezone_doc, /* tp_doc */ |
| 3395 | 0, /* tp_traverse */ |
| 3396 | 0, /* tp_clear */ |
| 3397 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3398 | 0, /* tp_weaklistoffset */ |
| 3399 | 0, /* tp_iter */ |
| 3400 | 0, /* tp_iternext */ |
| 3401 | timezone_methods, /* tp_methods */ |
| 3402 | 0, /* tp_members */ |
| 3403 | 0, /* tp_getset */ |
| 3404 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3405 | 0, /* tp_dict */ |
| 3406 | 0, /* tp_descr_get */ |
| 3407 | 0, /* tp_descr_set */ |
| 3408 | 0, /* tp_dictoffset */ |
| 3409 | 0, /* tp_init */ |
| 3410 | 0, /* tp_alloc */ |
| 3411 | timezone_new, /* tp_new */ |
| 3412 | }; |
| 3413 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3414 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3415 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3416 | */ |
| 3417 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3418 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3419 | */ |
| 3420 | |
| 3421 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3422 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3427 | static PyObject * |
| 3428 | time_minute(PyDateTime_Time *self, void *unused) |
| 3429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | /* The name time_second conflicted with some platform header file. */ |
| 3434 | static PyObject * |
| 3435 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | static PyObject * |
| 3441 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3443 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | static PyObject * |
| 3447 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3448 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3450 | Py_INCREF(result); |
| 3451 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | {"hour", (getter)time_hour}, |
| 3456 | {"minute", (getter)time_minute}, |
| 3457 | {"second", (getter)py_time_second}, |
| 3458 | {"microsecond", (getter)time_microsecond}, |
| 3459 | {"tzinfo", (getter)time_tzinfo}, |
| 3460 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3461 | }; |
| 3462 | |
| 3463 | /* |
| 3464 | * Constructors. |
| 3465 | */ |
| 3466 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3467 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | "tzinfo", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3469 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3470 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3471 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | PyObject *self = NULL; |
| 3474 | PyObject *state; |
| 3475 | int hour = 0; |
| 3476 | int minute = 0; |
| 3477 | int second = 0; |
| 3478 | int usecond = 0; |
| 3479 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | /* Check for invocation from pickle with __getstate__ state */ |
| 3482 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3483 | PyTuple_GET_SIZE(args) <= 2 && |
| 3484 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3485 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 3486 | ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24) |
| 3487 | { |
| 3488 | PyDateTime_Time *me; |
| 3489 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3492 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3493 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3494 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3495 | "tzinfo state arg"); |
| 3496 | return NULL; |
| 3497 | } |
| 3498 | } |
| 3499 | aware = (char)(tzinfo != Py_None); |
| 3500 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3501 | if (me != NULL) { |
| 3502 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3505 | me->hashcode = -1; |
| 3506 | me->hastzinfo = aware; |
| 3507 | if (aware) { |
| 3508 | Py_INCREF(tzinfo); |
| 3509 | me->tzinfo = tzinfo; |
| 3510 | } |
| 3511 | } |
| 3512 | return (PyObject *)me; |
| 3513 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
| 3516 | &hour, &minute, &second, &usecond, |
| 3517 | &tzinfo)) { |
| 3518 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3519 | return NULL; |
| 3520 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3521 | return NULL; |
| 3522 | self = new_time_ex(hour, minute, second, usecond, tzinfo, |
| 3523 | type); |
| 3524 | } |
| 3525 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /* |
| 3529 | * Destructor. |
| 3530 | */ |
| 3531 | |
| 3532 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3533 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3535 | if (HASTZINFO(self)) { |
| 3536 | Py_XDECREF(self->tzinfo); |
| 3537 | } |
| 3538 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3542 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3543 | */ |
| 3544 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3545 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3546 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3547 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3548 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3552 | time_dst(PyObject *self, PyObject *unused) { |
| 3553 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3557 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3558 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3562 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3563 | */ |
| 3564 | |
| 3565 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3566 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3569 | int h = TIME_GET_HOUR(self); |
| 3570 | int m = TIME_GET_MINUTE(self); |
| 3571 | int s = TIME_GET_SECOND(self); |
| 3572 | int us = TIME_GET_MICROSECOND(self); |
| 3573 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | if (us) |
| 3576 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3577 | type_name, h, m, s, us); |
| 3578 | else if (s) |
| 3579 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3580 | type_name, h, m, s); |
| 3581 | else |
| 3582 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3583 | if (result != NULL && HASTZINFO(self)) |
| 3584 | result = append_keyword_tzinfo(result, self->tzinfo); |
| 3585 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3588 | static PyObject * |
| 3589 | time_str(PyDateTime_Time *self) |
| 3590 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3591 | _Py_IDENTIFIER(isoformat); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3592 | |
| 3593 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3594 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3595 | |
| 3596 | static PyObject * |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3597 | time_isoformat(PyDateTime_Time *self, PyObject *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | char buf[100]; |
| 3600 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3601 | int us = TIME_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | if (us) |
| 3604 | result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d", |
| 3605 | TIME_GET_HOUR(self), |
| 3606 | TIME_GET_MINUTE(self), |
| 3607 | TIME_GET_SECOND(self), |
| 3608 | us); |
| 3609 | else |
| 3610 | result = PyUnicode_FromFormat("%02d:%02d:%02d", |
| 3611 | TIME_GET_HOUR(self), |
| 3612 | TIME_GET_MINUTE(self), |
| 3613 | TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3614 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3615 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | /* We need to append the UTC offset. */ |
| 3619 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3620 | Py_None) < 0) { |
| 3621 | Py_DECREF(result); |
| 3622 | return NULL; |
| 3623 | } |
| 3624 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3625 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3628 | static PyObject * |
| 3629 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | PyObject *result; |
| 3632 | PyObject *tuple; |
| 3633 | PyObject *format; |
| 3634 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3636 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3637 | &format)) |
| 3638 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | /* Python's strftime does insane things with the year part of the |
| 3641 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3642 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | */ |
| 3644 | tuple = Py_BuildValue("iiiiiiiii", |
| 3645 | 1900, 1, 1, /* year, month, day */ |
| 3646 | TIME_GET_HOUR(self), |
| 3647 | TIME_GET_MINUTE(self), |
| 3648 | TIME_GET_SECOND(self), |
| 3649 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3650 | if (tuple == NULL) |
| 3651 | return NULL; |
| 3652 | assert(PyTuple_Size(tuple) == 9); |
| 3653 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3654 | Py_None); |
| 3655 | Py_DECREF(tuple); |
| 3656 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3657 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3658 | |
| 3659 | /* |
| 3660 | * Miscellaneous methods. |
| 3661 | */ |
| 3662 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3663 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3664 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3665 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3666 | PyObject *result = NULL; |
| 3667 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3669 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3670 | if (! PyTime_Check(other)) |
| 3671 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3672 | |
| 3673 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3675 | ((PyDateTime_Time *)other)->data, |
| 3676 | _PyDateTime_TIME_DATASIZE); |
| 3677 | return diff_to_bool(diff, op); |
| 3678 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3679 | offset1 = time_utcoffset(self, NULL); |
| 3680 | if (offset1 == NULL) |
| 3681 | return NULL; |
| 3682 | offset2 = time_utcoffset(other, NULL); |
| 3683 | if (offset2 == NULL) |
| 3684 | goto done; |
| 3685 | /* If they're both naive, or both aware and have the same offsets, |
| 3686 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3687 | * offset2 == Py_None at this point. |
| 3688 | */ |
| 3689 | if ((offset1 == offset2) || |
| 3690 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 3691 | delta_cmp(offset1, offset2) == 0)) { |
| 3692 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3693 | ((PyDateTime_Time *)other)->data, |
| 3694 | _PyDateTime_TIME_DATASIZE); |
| 3695 | result = diff_to_bool(diff, op); |
| 3696 | } |
| 3697 | /* The hard case: both aware with different UTC offsets */ |
| 3698 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 3699 | int offsecs1, offsecs2; |
| 3700 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3701 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 3702 | TIME_GET_MINUTE(self) * 60 + |
| 3703 | TIME_GET_SECOND(self) - |
| 3704 | GET_TD_DAYS(offset1) * 86400 - |
| 3705 | GET_TD_SECONDS(offset1); |
| 3706 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 3707 | TIME_GET_MINUTE(other) * 60 + |
| 3708 | TIME_GET_SECOND(other) - |
| 3709 | GET_TD_DAYS(offset2) * 86400 - |
| 3710 | GET_TD_SECONDS(offset2); |
| 3711 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3712 | if (diff == 0) |
| 3713 | diff = TIME_GET_MICROSECOND(self) - |
| 3714 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3715 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3716 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3717 | else if (op == Py_EQ) { |
| 3718 | result = Py_False; |
| 3719 | Py_INCREF(result); |
| 3720 | } |
| 3721 | else if (op == Py_NE) { |
| 3722 | result = Py_True; |
| 3723 | Py_INCREF(result); |
| 3724 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3725 | else { |
| 3726 | PyErr_SetString(PyExc_TypeError, |
| 3727 | "can't compare offset-naive and " |
| 3728 | "offset-aware times"); |
| 3729 | } |
| 3730 | done: |
| 3731 | Py_DECREF(offset1); |
| 3732 | Py_XDECREF(offset2); |
| 3733 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3736 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3737 | time_hash(PyDateTime_Time *self) |
| 3738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3740 | PyObject *offset; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3741 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3742 | offset = time_utcoffset((PyObject *)self, NULL); |
| 3743 | |
| 3744 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3747 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3748 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | self->hashcode = generic_hash( |
| 3750 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3751 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3752 | PyObject *temp1, *temp2; |
| 3753 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3755 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 3756 | TIME_GET_MINUTE(self) * 60 + |
| 3757 | TIME_GET_SECOND(self); |
| 3758 | microseconds = TIME_GET_MICROSECOND(self); |
| 3759 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 3760 | if (temp1 == NULL) { |
| 3761 | Py_DECREF(offset); |
| 3762 | return -1; |
| 3763 | } |
| 3764 | temp2 = delta_subtract(temp1, offset); |
| 3765 | Py_DECREF(temp1); |
| 3766 | if (temp2 == NULL) { |
| 3767 | Py_DECREF(offset); |
| 3768 | return -1; |
| 3769 | } |
| 3770 | self->hashcode = PyObject_Hash(temp2); |
| 3771 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3773 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | } |
| 3775 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3776 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3777 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3778 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3779 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3781 | PyObject *clone; |
| 3782 | PyObject *tuple; |
| 3783 | int hh = TIME_GET_HOUR(self); |
| 3784 | int mm = TIME_GET_MINUTE(self); |
| 3785 | int ss = TIME_GET_SECOND(self); |
| 3786 | int us = TIME_GET_MICROSECOND(self); |
| 3787 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3789 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
| 3790 | time_kws, |
| 3791 | &hh, &mm, &ss, &us, &tzinfo)) |
| 3792 | return NULL; |
| 3793 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3794 | if (tuple == NULL) |
| 3795 | return NULL; |
| 3796 | clone = time_new(Py_TYPE(self), tuple, NULL); |
| 3797 | Py_DECREF(tuple); |
| 3798 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3801 | static int |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3802 | time_bool(PyObject *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3803 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3804 | PyObject *offset, *tzinfo; |
| 3805 | int offsecs = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3807 | if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { |
| 3808 | /* Since utcoffset is in whole minutes, nothing can |
| 3809 | * alter the conclusion that this is nonzero. |
| 3810 | */ |
| 3811 | return 1; |
| 3812 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3813 | tzinfo = GET_TIME_TZINFO(self); |
| 3814 | if (tzinfo != Py_None) { |
| 3815 | offset = call_utcoffset(tzinfo, Py_None); |
| 3816 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3818 | offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset); |
| 3819 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3821 | return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3824 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3825 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3826 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3827 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3828 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3829 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3830 | */ |
| 3831 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3832 | time_getstate(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3833 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3834 | PyObject *basestate; |
| 3835 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3838 | _PyDateTime_TIME_DATASIZE); |
| 3839 | if (basestate != NULL) { |
| 3840 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3841 | result = PyTuple_Pack(1, basestate); |
| 3842 | else |
| 3843 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3844 | Py_DECREF(basestate); |
| 3845 | } |
| 3846 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3850 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3852 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3855 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
| 3858 | PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
| 3859 | "[+HH:MM].")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3862 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3864 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3865 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 3868 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3870 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 3871 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 3874 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3876 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 3877 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3879 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 3880 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3883 | }; |
| 3884 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3885 | static char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3886 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 3887 | \n\ |
| 3888 | All arguments are optional. tzinfo may be None, or an instance of\n\ |
| 3889 | a tzinfo subclass. The remaining arguments may be ints or longs.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3890 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3891 | static PyNumberMethods time_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3892 | 0, /* nb_add */ |
| 3893 | 0, /* nb_subtract */ |
| 3894 | 0, /* nb_multiply */ |
| 3895 | 0, /* nb_remainder */ |
| 3896 | 0, /* nb_divmod */ |
| 3897 | 0, /* nb_power */ |
| 3898 | 0, /* nb_negative */ |
| 3899 | 0, /* nb_positive */ |
| 3900 | 0, /* nb_absolute */ |
| 3901 | (inquiry)time_bool, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3902 | }; |
| 3903 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3904 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3905 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3906 | "datetime.time", /* tp_name */ |
| 3907 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 3908 | 0, /* tp_itemsize */ |
| 3909 | (destructor)time_dealloc, /* tp_dealloc */ |
| 3910 | 0, /* tp_print */ |
| 3911 | 0, /* tp_getattr */ |
| 3912 | 0, /* tp_setattr */ |
| 3913 | 0, /* tp_reserved */ |
| 3914 | (reprfunc)time_repr, /* tp_repr */ |
| 3915 | &time_as_number, /* tp_as_number */ |
| 3916 | 0, /* tp_as_sequence */ |
| 3917 | 0, /* tp_as_mapping */ |
| 3918 | (hashfunc)time_hash, /* tp_hash */ |
| 3919 | 0, /* tp_call */ |
| 3920 | (reprfunc)time_str, /* tp_str */ |
| 3921 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3922 | 0, /* tp_setattro */ |
| 3923 | 0, /* tp_as_buffer */ |
| 3924 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3925 | time_doc, /* tp_doc */ |
| 3926 | 0, /* tp_traverse */ |
| 3927 | 0, /* tp_clear */ |
| 3928 | time_richcompare, /* tp_richcompare */ |
| 3929 | 0, /* tp_weaklistoffset */ |
| 3930 | 0, /* tp_iter */ |
| 3931 | 0, /* tp_iternext */ |
| 3932 | time_methods, /* tp_methods */ |
| 3933 | 0, /* tp_members */ |
| 3934 | time_getset, /* tp_getset */ |
| 3935 | 0, /* tp_base */ |
| 3936 | 0, /* tp_dict */ |
| 3937 | 0, /* tp_descr_get */ |
| 3938 | 0, /* tp_descr_set */ |
| 3939 | 0, /* tp_dictoffset */ |
| 3940 | 0, /* tp_init */ |
| 3941 | time_alloc, /* tp_alloc */ |
| 3942 | time_new, /* tp_new */ |
| 3943 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3944 | }; |
| 3945 | |
| 3946 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3947 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3948 | */ |
| 3949 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3950 | /* Accessor properties. Properties for day, month, and year are inherited |
| 3951 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3952 | */ |
| 3953 | |
| 3954 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3955 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3960 | static PyObject * |
| 3961 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 3962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3963 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | static PyObject * |
| 3967 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 3968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | static PyObject * |
| 3973 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 3974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | static PyObject * |
| 3979 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 3980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3981 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3982 | Py_INCREF(result); |
| 3983 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3987 | {"hour", (getter)datetime_hour}, |
| 3988 | {"minute", (getter)datetime_minute}, |
| 3989 | {"second", (getter)datetime_second}, |
| 3990 | {"microsecond", (getter)datetime_microsecond}, |
| 3991 | {"tzinfo", (getter)datetime_tzinfo}, |
| 3992 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3993 | }; |
| 3994 | |
| 3995 | /* |
| 3996 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3997 | */ |
| 3998 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3999 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4000 | "year", "month", "day", "hour", "minute", "second", |
| 4001 | "microsecond", "tzinfo", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4002 | }; |
| 4003 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4004 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4005 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4006 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4007 | PyObject *self = NULL; |
| 4008 | PyObject *state; |
| 4009 | int year; |
| 4010 | int month; |
| 4011 | int day; |
| 4012 | int hour = 0; |
| 4013 | int minute = 0; |
| 4014 | int second = 0; |
| 4015 | int usecond = 0; |
| 4016 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4018 | /* Check for invocation from pickle with __getstate__ state */ |
| 4019 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 4020 | PyTuple_GET_SIZE(args) <= 2 && |
| 4021 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 4022 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4023 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 4024 | { |
| 4025 | PyDateTime_DateTime *me; |
| 4026 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4028 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4029 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4030 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4031 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4032 | "tzinfo state arg"); |
| 4033 | return NULL; |
| 4034 | } |
| 4035 | } |
| 4036 | aware = (char)(tzinfo != Py_None); |
| 4037 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4038 | if (me != NULL) { |
| 4039 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4042 | me->hashcode = -1; |
| 4043 | me->hastzinfo = aware; |
| 4044 | if (aware) { |
| 4045 | Py_INCREF(tzinfo); |
| 4046 | me->tzinfo = tzinfo; |
| 4047 | } |
| 4048 | } |
| 4049 | return (PyObject *)me; |
| 4050 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
| 4053 | &year, &month, &day, &hour, &minute, |
| 4054 | &second, &usecond, &tzinfo)) { |
| 4055 | if (check_date_args(year, month, day) < 0) |
| 4056 | return NULL; |
| 4057 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 4058 | return NULL; |
| 4059 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4060 | return NULL; |
| 4061 | self = new_datetime_ex(year, month, day, |
| 4062 | hour, minute, second, usecond, |
| 4063 | tzinfo, type); |
| 4064 | } |
| 4065 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4068 | /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
| 4069 | typedef struct tm *(*TM_FUNC)(const time_t *timer); |
| 4070 | |
| 4071 | /* Internal helper. |
| 4072 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4073 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4074 | */ |
| 4075 | static PyObject * |
| 4076 | datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | struct tm *tm; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4080 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4081 | tm = f(&timet); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4082 | if (tm == NULL) { |
| 4083 | #ifdef EINVAL |
| 4084 | if (errno == 0) |
| 4085 | errno = EINVAL; |
| 4086 | #endif |
| 4087 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4088 | } |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4089 | |
| 4090 | /* The platform localtime/gmtime may insert leap seconds, |
| 4091 | * indicated by tm->tm_sec > 59. We don't care about them, |
| 4092 | * except to the extent that passing them on to the datetime |
| 4093 | * constructor would raise ValueError for a reason that |
| 4094 | * made no sense to the user. |
| 4095 | */ |
| 4096 | if (tm->tm_sec > 59) |
| 4097 | tm->tm_sec = 59; |
| 4098 | return PyObject_CallFunction(cls, "iiiiiiiO", |
| 4099 | tm->tm_year + 1900, |
| 4100 | tm->tm_mon + 1, |
| 4101 | tm->tm_mday, |
| 4102 | tm->tm_hour, |
| 4103 | tm->tm_min, |
| 4104 | tm->tm_sec, |
| 4105 | us, |
| 4106 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | /* Internal helper. |
| 4110 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4111 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4112 | * have enough bits to cover a datetime's full range of precision, it's |
| 4113 | * better to call datetime_from_timet_and_us provided you have a way |
| 4114 | * to get that much precision (e.g., C time() isn't good enough). |
| 4115 | */ |
| 4116 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4117 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4118 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4121 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4122 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4123 | if (_PyTime_ObjectToTimeval(timestamp, &timet, &us) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4124 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4125 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | /* Internal helper. |
| 4129 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4130 | * gmtime for f as appropriate. |
| 4131 | */ |
| 4132 | static PyObject * |
| 4133 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4134 | { |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 4135 | _PyTime_timeval t; |
| 4136 | _PyTime_gettimeofday(&t); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, |
| 4138 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4141 | /* Return best possible local time -- this isn't constrained by the |
| 4142 | * precision of a timestamp. |
| 4143 | */ |
| 4144 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4145 | datetime_now(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | PyObject *self; |
| 4148 | PyObject *tzinfo = Py_None; |
| 4149 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4151 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords, |
| 4152 | &tzinfo)) |
| 4153 | return NULL; |
| 4154 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4155 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4157 | self = datetime_best_possible(cls, |
| 4158 | tzinfo == Py_None ? localtime : gmtime, |
| 4159 | tzinfo); |
| 4160 | if (self != NULL && tzinfo != Py_None) { |
| 4161 | /* Convert UTC to tzinfo's zone. */ |
| 4162 | PyObject *temp = self; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4163 | _Py_IDENTIFIER(fromutc); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4164 | |
| 4165 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | Py_DECREF(temp); |
| 4167 | } |
| 4168 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4169 | } |
| 4170 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4171 | /* Return best possible UTC time -- this isn't constrained by the |
| 4172 | * precision of a timestamp. |
| 4173 | */ |
| 4174 | static PyObject * |
| 4175 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 | return datetime_best_possible(cls, gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4180 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4181 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4182 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4185 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | PyObject *tzinfo = Py_None; |
| 4187 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4188 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4189 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | keywords, ×tamp, &tzinfo)) |
| 4191 | return NULL; |
| 4192 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4193 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4195 | self = datetime_from_timestamp(cls, |
| 4196 | tzinfo == Py_None ? localtime : gmtime, |
| 4197 | timestamp, |
| 4198 | tzinfo); |
| 4199 | if (self != NULL && tzinfo != Py_None) { |
| 4200 | /* Convert UTC to tzinfo's zone. */ |
| 4201 | PyObject *temp = self; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4202 | _Py_IDENTIFIER(fromutc); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4203 | |
| 4204 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | Py_DECREF(temp); |
| 4206 | } |
| 4207 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4210 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4211 | static PyObject * |
| 4212 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4213 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4214 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4215 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4216 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4217 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4218 | result = datetime_from_timestamp(cls, gmtime, timestamp, |
| 4219 | Py_None); |
| 4220 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4223 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4224 | static PyObject * |
| 4225 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4227 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4228 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4229 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4230 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4231 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4233 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4234 | if (module == NULL) { |
| 4235 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4236 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4237 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4239 | return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO", |
| 4240 | cls, string, format); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4243 | /* Return new datetime from date/datetime and time arguments. */ |
| 4244 | static PyObject * |
| 4245 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | static char *keywords[] = {"date", "time", NULL}; |
| 4248 | PyObject *date; |
| 4249 | PyObject *time; |
| 4250 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
| 4253 | &PyDateTime_DateType, &date, |
| 4254 | &PyDateTime_TimeType, &time)) { |
| 4255 | PyObject *tzinfo = Py_None; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4257 | if (HASTZINFO(time)) |
| 4258 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4259 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 4260 | GET_YEAR(date), |
| 4261 | GET_MONTH(date), |
| 4262 | GET_DAY(date), |
| 4263 | TIME_GET_HOUR(time), |
| 4264 | TIME_GET_MINUTE(time), |
| 4265 | TIME_GET_SECOND(time), |
| 4266 | TIME_GET_MICROSECOND(time), |
| 4267 | tzinfo); |
| 4268 | } |
| 4269 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4270 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4271 | |
| 4272 | /* |
| 4273 | * Destructor. |
| 4274 | */ |
| 4275 | |
| 4276 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4277 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4278 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | if (HASTZINFO(self)) { |
| 4280 | Py_XDECREF(self->tzinfo); |
| 4281 | } |
| 4282 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | /* |
| 4286 | * Indirect access to tzinfo methods. |
| 4287 | */ |
| 4288 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4289 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4290 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4291 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4292 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4296 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4297 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4301 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4302 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4306 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4307 | */ |
| 4308 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4309 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4310 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4311 | */ |
| 4312 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4313 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | /* Note that the C-level additions can't overflow, because of |
| 4317 | * invariant bounds on the member values. |
| 4318 | */ |
| 4319 | int year = GET_YEAR(date); |
| 4320 | int month = GET_MONTH(date); |
| 4321 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4322 | int hour = DATE_GET_HOUR(date); |
| 4323 | int minute = DATE_GET_MINUTE(date); |
| 4324 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4325 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4326 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | assert(factor == 1 || factor == -1); |
| 4329 | if (normalize_datetime(&year, &month, &day, |
| 4330 | &hour, &minute, &second, µsecond) < 0) |
| 4331 | return NULL; |
| 4332 | else |
| 4333 | return new_datetime(year, month, day, |
| 4334 | hour, minute, second, microsecond, |
| 4335 | HASTZINFO(date) ? date->tzinfo : Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4339 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4341 | if (PyDateTime_Check(left)) { |
| 4342 | /* datetime + ??? */ |
| 4343 | if (PyDelta_Check(right)) |
| 4344 | /* datetime + delta */ |
| 4345 | return add_datetime_timedelta( |
| 4346 | (PyDateTime_DateTime *)left, |
| 4347 | (PyDateTime_Delta *)right, |
| 4348 | 1); |
| 4349 | } |
| 4350 | else if (PyDelta_Check(left)) { |
| 4351 | /* delta + datetime */ |
| 4352 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4353 | (PyDateTime_Delta *) left, |
| 4354 | 1); |
| 4355 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4356 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4360 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4362 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | if (PyDateTime_Check(left)) { |
| 4365 | /* datetime - ??? */ |
| 4366 | if (PyDateTime_Check(right)) { |
| 4367 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4368 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4370 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4371 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 4372 | offset2 = offset1 = Py_None; |
| 4373 | Py_INCREF(offset1); |
| 4374 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4376 | else { |
| 4377 | offset1 = datetime_utcoffset(left, NULL); |
| 4378 | if (offset1 == NULL) |
| 4379 | return NULL; |
| 4380 | offset2 = datetime_utcoffset(right, NULL); |
| 4381 | if (offset2 == NULL) { |
| 4382 | Py_DECREF(offset1); |
| 4383 | return NULL; |
| 4384 | } |
| 4385 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 4386 | PyErr_SetString(PyExc_TypeError, |
| 4387 | "can't subtract offset-naive and " |
| 4388 | "offset-aware datetimes"); |
| 4389 | Py_DECREF(offset1); |
| 4390 | Py_DECREF(offset2); |
| 4391 | return NULL; |
| 4392 | } |
| 4393 | } |
| 4394 | if ((offset1 != offset2) && |
| 4395 | delta_cmp(offset1, offset2) != 0) { |
| 4396 | offdiff = delta_subtract(offset1, offset2); |
| 4397 | if (offdiff == NULL) { |
| 4398 | Py_DECREF(offset1); |
| 4399 | Py_DECREF(offset2); |
| 4400 | return NULL; |
| 4401 | } |
| 4402 | } |
| 4403 | Py_DECREF(offset1); |
| 4404 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4405 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4406 | GET_MONTH(left), |
| 4407 | GET_DAY(left)) - |
| 4408 | ymd_to_ord(GET_YEAR(right), |
| 4409 | GET_MONTH(right), |
| 4410 | GET_DAY(right)); |
| 4411 | /* These can't overflow, since the values are |
| 4412 | * normalized. At most this gives the number of |
| 4413 | * seconds in one day. |
| 4414 | */ |
| 4415 | delta_s = (DATE_GET_HOUR(left) - |
| 4416 | DATE_GET_HOUR(right)) * 3600 + |
| 4417 | (DATE_GET_MINUTE(left) - |
| 4418 | DATE_GET_MINUTE(right)) * 60 + |
| 4419 | (DATE_GET_SECOND(left) - |
| 4420 | DATE_GET_SECOND(right)); |
| 4421 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4422 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4424 | if (offdiff != NULL) { |
| 4425 | PyObject *temp = result; |
| 4426 | result = delta_subtract(result, offdiff); |
| 4427 | Py_DECREF(temp); |
| 4428 | Py_DECREF(offdiff); |
| 4429 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | } |
| 4431 | else if (PyDelta_Check(right)) { |
| 4432 | /* datetime - delta */ |
| 4433 | result = add_datetime_timedelta( |
| 4434 | (PyDateTime_DateTime *)left, |
| 4435 | (PyDateTime_Delta *)right, |
| 4436 | -1); |
| 4437 | } |
| 4438 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4440 | if (result == Py_NotImplemented) |
| 4441 | Py_INCREF(result); |
| 4442 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | /* Various ways to turn a datetime into a string. */ |
| 4446 | |
| 4447 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4448 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4450 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4451 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | if (DATE_GET_MICROSECOND(self)) { |
| 4454 | baserepr = PyUnicode_FromFormat( |
| 4455 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4456 | type_name, |
| 4457 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4458 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4459 | DATE_GET_SECOND(self), |
| 4460 | DATE_GET_MICROSECOND(self)); |
| 4461 | } |
| 4462 | else if (DATE_GET_SECOND(self)) { |
| 4463 | baserepr = PyUnicode_FromFormat( |
| 4464 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4465 | type_name, |
| 4466 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4467 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4468 | DATE_GET_SECOND(self)); |
| 4469 | } |
| 4470 | else { |
| 4471 | baserepr = PyUnicode_FromFormat( |
| 4472 | "%s(%d, %d, %d, %d, %d)", |
| 4473 | type_name, |
| 4474 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4475 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4476 | } |
| 4477 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4478 | return baserepr; |
| 4479 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4482 | static PyObject * |
| 4483 | datetime_str(PyDateTime_DateTime *self) |
| 4484 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4485 | _Py_IDENTIFIER(isoformat); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4486 | |
| 4487 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "(s)", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4488 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4489 | |
| 4490 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4491 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | int sep = 'T'; |
| 4494 | static char *keywords[] = {"sep", NULL}; |
| 4495 | char buffer[100]; |
| 4496 | PyObject *result; |
| 4497 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep)) |
| 4500 | return NULL; |
| 4501 | if (us) |
| 4502 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d", |
| 4503 | GET_YEAR(self), GET_MONTH(self), |
| 4504 | GET_DAY(self), (int)sep, |
| 4505 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4506 | DATE_GET_SECOND(self), us); |
| 4507 | else |
| 4508 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d", |
| 4509 | GET_YEAR(self), GET_MONTH(self), |
| 4510 | GET_DAY(self), (int)sep, |
| 4511 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4512 | DATE_GET_SECOND(self)); |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | if (!result || !HASTZINFO(self)) |
| 4515 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | /* We need to append the UTC offset. */ |
| 4518 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4519 | (PyObject *)self) < 0) { |
| 4520 | Py_DECREF(result); |
| 4521 | return NULL; |
| 4522 | } |
| 4523 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4524 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4527 | static PyObject * |
| 4528 | datetime_ctime(PyDateTime_DateTime *self) |
| 4529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | return format_ctime((PyDateTime_Date *)self, |
| 4531 | DATE_GET_HOUR(self), |
| 4532 | DATE_GET_MINUTE(self), |
| 4533 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4536 | /* Miscellaneous methods. */ |
| 4537 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4538 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4539 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4540 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4541 | PyObject *result = NULL; |
| 4542 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | if (! PyDateTime_Check(other)) { |
| 4546 | if (PyDate_Check(other)) { |
| 4547 | /* Prevent invocation of date_richcompare. We want to |
| 4548 | return NotImplemented here to give the other object |
| 4549 | a chance. But since DateTime is a subclass of |
| 4550 | Date, if the other object is a Date, it would |
| 4551 | compute an ordering based on the date part alone, |
| 4552 | and we don't want that. So force unequal or |
| 4553 | uncomparable here in that case. */ |
| 4554 | if (op == Py_EQ) |
| 4555 | Py_RETURN_FALSE; |
| 4556 | if (op == Py_NE) |
| 4557 | Py_RETURN_TRUE; |
| 4558 | return cmperror(self, other); |
| 4559 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4560 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4562 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4563 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4565 | ((PyDateTime_DateTime *)other)->data, |
| 4566 | _PyDateTime_DATETIME_DATASIZE); |
| 4567 | return diff_to_bool(diff, op); |
| 4568 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4569 | offset1 = datetime_utcoffset(self, NULL); |
| 4570 | if (offset1 == NULL) |
| 4571 | return NULL; |
| 4572 | offset2 = datetime_utcoffset(other, NULL); |
| 4573 | if (offset2 == NULL) |
| 4574 | goto done; |
| 4575 | /* If they're both naive, or both aware and have the same offsets, |
| 4576 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4577 | * offset2 == Py_None at this point. |
| 4578 | */ |
| 4579 | if ((offset1 == offset2) || |
| 4580 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4581 | delta_cmp(offset1, offset2) == 0)) { |
| 4582 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4583 | ((PyDateTime_DateTime *)other)->data, |
| 4584 | _PyDateTime_DATETIME_DATASIZE); |
| 4585 | result = diff_to_bool(diff, op); |
| 4586 | } |
| 4587 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4589 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4590 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4592 | other); |
| 4593 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4594 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4595 | diff = GET_TD_DAYS(delta); |
| 4596 | if (diff == 0) |
| 4597 | diff = GET_TD_SECONDS(delta) | |
| 4598 | GET_TD_MICROSECONDS(delta); |
| 4599 | Py_DECREF(delta); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4600 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4602 | else if (op == Py_EQ) { |
| 4603 | result = Py_False; |
| 4604 | Py_INCREF(result); |
| 4605 | } |
| 4606 | else if (op == Py_NE) { |
| 4607 | result = Py_True; |
| 4608 | Py_INCREF(result); |
| 4609 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4610 | else { |
| 4611 | PyErr_SetString(PyExc_TypeError, |
| 4612 | "can't compare offset-naive and " |
| 4613 | "offset-aware datetimes"); |
| 4614 | } |
| 4615 | done: |
| 4616 | Py_DECREF(offset1); |
| 4617 | Py_XDECREF(offset2); |
| 4618 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4621 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4622 | datetime_hash(PyDateTime_DateTime *self) |
| 4623 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4625 | PyObject *offset; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4626 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4627 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4628 | |
| 4629 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4633 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | self->hashcode = generic_hash( |
| 4635 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4637 | PyObject *temp1, *temp2; |
| 4638 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | assert(HASTZINFO(self)); |
| 4641 | days = ymd_to_ord(GET_YEAR(self), |
| 4642 | GET_MONTH(self), |
| 4643 | GET_DAY(self)); |
| 4644 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4645 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4647 | temp1 = new_delta(days, seconds, |
| 4648 | DATE_GET_MICROSECOND(self), |
| 4649 | 1); |
| 4650 | if (temp1 == NULL) { |
| 4651 | Py_DECREF(offset); |
| 4652 | return -1; |
| 4653 | } |
| 4654 | temp2 = delta_subtract(temp1, offset); |
| 4655 | Py_DECREF(temp1); |
| 4656 | if (temp2 == NULL) { |
| 4657 | Py_DECREF(offset); |
| 4658 | return -1; |
| 4659 | } |
| 4660 | self->hashcode = PyObject_Hash(temp2); |
| 4661 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4662 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4663 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | } |
| 4665 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4666 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4667 | |
| 4668 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4669 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | PyObject *clone; |
| 4672 | PyObject *tuple; |
| 4673 | int y = GET_YEAR(self); |
| 4674 | int m = GET_MONTH(self); |
| 4675 | int d = GET_DAY(self); |
| 4676 | int hh = DATE_GET_HOUR(self); |
| 4677 | int mm = DATE_GET_MINUTE(self); |
| 4678 | int ss = DATE_GET_SECOND(self); |
| 4679 | int us = DATE_GET_MICROSECOND(self); |
| 4680 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
| 4683 | datetime_kws, |
| 4684 | &y, &m, &d, &hh, &mm, &ss, &us, |
| 4685 | &tzinfo)) |
| 4686 | return NULL; |
| 4687 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 4688 | if (tuple == NULL) |
| 4689 | return NULL; |
| 4690 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
| 4691 | Py_DECREF(tuple); |
| 4692 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | static PyObject * |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4696 | local_timezone(PyDateTime_DateTime *utc_time) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4697 | { |
| 4698 | PyObject *result = NULL; |
| 4699 | struct tm *timep; |
| 4700 | time_t timestamp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4701 | PyObject *delta; |
| 4702 | PyObject *one_second; |
| 4703 | PyObject *seconds; |
| 4704 | PyObject *nameo = NULL; |
| 4705 | const char *zone = NULL; |
| 4706 | |
| 4707 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 4708 | if (delta == NULL) |
| 4709 | return NULL; |
| 4710 | one_second = new_delta(0, 1, 0, 0); |
| 4711 | if (one_second == NULL) |
| 4712 | goto error; |
| 4713 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 4714 | (PyDateTime_Delta *)one_second); |
| 4715 | Py_DECREF(one_second); |
| 4716 | if (seconds == NULL) |
| 4717 | goto error; |
| 4718 | Py_DECREF(delta); |
| 4719 | timestamp = PyLong_AsLong(seconds); |
| 4720 | Py_DECREF(seconds); |
| 4721 | if (timestamp == -1 && PyErr_Occurred()) |
| 4722 | return NULL; |
| 4723 | timep = localtime(×tamp); |
| 4724 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4725 | zone = timep->tm_zone; |
| 4726 | delta = new_delta(0, timep->tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4727 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4728 | { |
| 4729 | PyObject *local_time; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4730 | local_time = new_datetime(timep->tm_year + 1900, timep->tm_mon + 1, |
| 4731 | timep->tm_mday, timep->tm_hour, timep->tm_min, |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4732 | timep->tm_sec, DATE_GET_MICROSECOND(utc_time), |
| 4733 | utc_time->tzinfo); |
| 4734 | if (local_time == NULL) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4735 | goto error; |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4736 | delta = datetime_subtract(local_time, (PyObject*)utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4737 | /* XXX: before relying on tzname, we should compare delta |
| 4738 | to the offset implied by timezone/altzone */ |
| 4739 | if (daylight && timep->tm_isdst >= 0) |
| 4740 | zone = tzname[timep->tm_isdst % 2]; |
| 4741 | else |
| 4742 | zone = tzname[0]; |
| 4743 | Py_DECREF(local_time); |
| 4744 | } |
| 4745 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4746 | if (zone != NULL) { |
| 4747 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 4748 | if (nameo == NULL) |
| 4749 | goto error; |
| 4750 | } |
| 4751 | result = new_timezone(delta, nameo); |
| 4752 | Py_DECREF(nameo); |
| 4753 | error: |
| 4754 | Py_DECREF(delta); |
| 4755 | return result; |
| 4756 | } |
| 4757 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4758 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4759 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4760 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4761 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4762 | PyObject *offset; |
| 4763 | PyObject *temp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4764 | PyObject *tzinfo = Py_None; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4765 | _Py_IDENTIFIER(fromutc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4767 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4768 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
| 4769 | &tzinfo)) |
| 4770 | return NULL; |
| 4771 | |
| 4772 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | if (!HASTZINFO(self) || self->tzinfo == Py_None) |
| 4776 | goto NeedAware; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | /* Conversion to self's own time zone is a NOP. */ |
| 4779 | if (self->tzinfo == tzinfo) { |
| 4780 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4781 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4782 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | /* Convert self to UTC. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4785 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4786 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4787 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4788 | if (offset == Py_None) { |
| 4789 | Py_DECREF(offset); |
| 4790 | NeedAware: |
| 4791 | PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
| 4792 | "a naive datetime"); |
| 4793 | return NULL; |
| 4794 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4795 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4796 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4797 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4798 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4799 | Py_DECREF(offset); |
| 4800 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4801 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4804 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4805 | if (tzinfo == Py_None) { |
| 4806 | tzinfo = local_timezone(result); |
| 4807 | if (tzinfo == NULL) { |
| 4808 | Py_DECREF(result); |
| 4809 | return NULL; |
| 4810 | } |
| 4811 | } |
| 4812 | else |
| 4813 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4814 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4815 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4816 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4817 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4818 | result = (PyDateTime_DateTime *) |
| 4819 | _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4820 | Py_DECREF(temp); |
| 4821 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4822 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4826 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4827 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4830 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4831 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4832 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4833 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 4834 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4835 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4836 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4837 | if (dst != Py_None) |
| 4838 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 4839 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | } |
| 4841 | return build_struct_time(GET_YEAR(self), |
| 4842 | GET_MONTH(self), |
| 4843 | GET_DAY(self), |
| 4844 | DATE_GET_HOUR(self), |
| 4845 | DATE_GET_MINUTE(self), |
| 4846 | DATE_GET_SECOND(self), |
| 4847 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | static PyObject * |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4851 | datetime_timestamp(PyDateTime_DateTime *self) |
| 4852 | { |
| 4853 | PyObject *result; |
| 4854 | |
| 4855 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 4856 | PyObject *delta; |
| 4857 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 4858 | if (delta == NULL) |
| 4859 | return NULL; |
| 4860 | result = delta_total_seconds(delta); |
| 4861 | Py_DECREF(delta); |
| 4862 | } |
| 4863 | else { |
| 4864 | struct tm time; |
| 4865 | time_t timestamp; |
| 4866 | memset((void *) &time, '\0', sizeof(struct tm)); |
| 4867 | time.tm_year = GET_YEAR(self) - 1900; |
| 4868 | time.tm_mon = GET_MONTH(self) - 1; |
| 4869 | time.tm_mday = GET_DAY(self); |
| 4870 | time.tm_hour = DATE_GET_HOUR(self); |
| 4871 | time.tm_min = DATE_GET_MINUTE(self); |
| 4872 | time.tm_sec = DATE_GET_SECOND(self); |
| 4873 | time.tm_wday = -1; |
| 4874 | time.tm_isdst = -1; |
| 4875 | timestamp = mktime(&time); |
| 4876 | /* Return value of -1 does not necessarily mean an error, but tm_wday |
| 4877 | * cannot remain set to -1 if mktime succeeded. */ |
| 4878 | if (timestamp == (time_t)(-1) && time.tm_wday == -1) { |
| 4879 | PyErr_SetString(PyExc_OverflowError, |
| 4880 | "timestamp out of range"); |
| 4881 | return NULL; |
| 4882 | } |
| 4883 | result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6); |
| 4884 | } |
| 4885 | return result; |
| 4886 | } |
| 4887 | |
| 4888 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4889 | datetime_getdate(PyDateTime_DateTime *self) |
| 4890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4891 | return new_date(GET_YEAR(self), |
| 4892 | GET_MONTH(self), |
| 4893 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | static PyObject * |
| 4897 | datetime_gettime(PyDateTime_DateTime *self) |
| 4898 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4899 | return new_time(DATE_GET_HOUR(self), |
| 4900 | DATE_GET_MINUTE(self), |
| 4901 | DATE_GET_SECOND(self), |
| 4902 | DATE_GET_MICROSECOND(self), |
| 4903 | Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | static PyObject * |
| 4907 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 4908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4909 | return new_time(DATE_GET_HOUR(self), |
| 4910 | DATE_GET_MINUTE(self), |
| 4911 | DATE_GET_SECOND(self), |
| 4912 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4913 | GET_DT_TZINFO(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | static PyObject * |
| 4917 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4918 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4919 | int y, m, d, hh, mm, ss; |
| 4920 | PyObject *tzinfo; |
| 4921 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4922 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4923 | tzinfo = GET_DT_TZINFO(self); |
| 4924 | if (tzinfo == Py_None) { |
| 4925 | utcself = self; |
| 4926 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4927 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4928 | else { |
| 4929 | PyObject *offset; |
| 4930 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 4931 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 4932 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4933 | if (offset == Py_None) { |
| 4934 | Py_DECREF(offset); |
| 4935 | utcself = self; |
| 4936 | Py_INCREF(utcself); |
| 4937 | } |
| 4938 | else { |
| 4939 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4940 | (PyDateTime_Delta *)offset, -1); |
| 4941 | Py_DECREF(offset); |
| 4942 | if (utcself == NULL) |
| 4943 | return NULL; |
| 4944 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4946 | y = GET_YEAR(utcself); |
| 4947 | m = GET_MONTH(utcself); |
| 4948 | d = GET_DAY(utcself); |
| 4949 | hh = DATE_GET_HOUR(utcself); |
| 4950 | mm = DATE_GET_MINUTE(utcself); |
| 4951 | ss = DATE_GET_SECOND(utcself); |
| 4952 | |
| 4953 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4957 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4958 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4959 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4960 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4961 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4962 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4963 | */ |
| 4964 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4965 | datetime_getstate(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4967 | PyObject *basestate; |
| 4968 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4971 | _PyDateTime_DATETIME_DATASIZE); |
| 4972 | if (basestate != NULL) { |
| 4973 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4974 | result = PyTuple_Pack(1, basestate); |
| 4975 | else |
| 4976 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4977 | Py_DECREF(basestate); |
| 4978 | } |
| 4979 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4983 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4984 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4988 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4990 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | {"now", (PyCFunction)datetime_now, |
| 4993 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 4994 | PyDoc_STR("[tz] -> new datetime with tz's local day and time.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 4997 | METH_NOARGS | METH_CLASS, |
| 4998 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5000 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 5001 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5002 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5005 | METH_VARARGS | METH_CLASS, |
| 5006 | PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " |
| 5007 | "(like time.time()).")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | {"strptime", (PyCFunction)datetime_strptime, |
| 5010 | METH_VARARGS | METH_CLASS, |
| 5011 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5012 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | {"combine", (PyCFunction)datetime_combine, |
| 5015 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5016 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5021 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5024 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5026 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5027 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5029 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5030 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5033 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5034 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5035 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5036 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5039 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5042 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
| 5043 | "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
| 5044 | "sep is used to separate the year from the time, and " |
| 5045 | "defaults to 'T'.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5048 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5051 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5053 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5054 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5057 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5059 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5060 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5063 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5066 | }; |
| 5067 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5068 | static char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5069 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5070 | \n\ |
| 5071 | The year, month and day arguments are required. tzinfo may be None, or an\n\ |
| 5072 | instance of a tzinfo subclass. The remaining arguments may be ints or longs.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5073 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5074 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5075 | datetime_add, /* nb_add */ |
| 5076 | datetime_subtract, /* nb_subtract */ |
| 5077 | 0, /* nb_multiply */ |
| 5078 | 0, /* nb_remainder */ |
| 5079 | 0, /* nb_divmod */ |
| 5080 | 0, /* nb_power */ |
| 5081 | 0, /* nb_negative */ |
| 5082 | 0, /* nb_positive */ |
| 5083 | 0, /* nb_absolute */ |
| 5084 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5085 | }; |
| 5086 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5087 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5088 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5089 | "datetime.datetime", /* tp_name */ |
| 5090 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 5091 | 0, /* tp_itemsize */ |
| 5092 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 5093 | 0, /* tp_print */ |
| 5094 | 0, /* tp_getattr */ |
| 5095 | 0, /* tp_setattr */ |
| 5096 | 0, /* tp_reserved */ |
| 5097 | (reprfunc)datetime_repr, /* tp_repr */ |
| 5098 | &datetime_as_number, /* tp_as_number */ |
| 5099 | 0, /* tp_as_sequence */ |
| 5100 | 0, /* tp_as_mapping */ |
| 5101 | (hashfunc)datetime_hash, /* tp_hash */ |
| 5102 | 0, /* tp_call */ |
| 5103 | (reprfunc)datetime_str, /* tp_str */ |
| 5104 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5105 | 0, /* tp_setattro */ |
| 5106 | 0, /* tp_as_buffer */ |
| 5107 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5108 | datetime_doc, /* tp_doc */ |
| 5109 | 0, /* tp_traverse */ |
| 5110 | 0, /* tp_clear */ |
| 5111 | datetime_richcompare, /* tp_richcompare */ |
| 5112 | 0, /* tp_weaklistoffset */ |
| 5113 | 0, /* tp_iter */ |
| 5114 | 0, /* tp_iternext */ |
| 5115 | datetime_methods, /* tp_methods */ |
| 5116 | 0, /* tp_members */ |
| 5117 | datetime_getset, /* tp_getset */ |
| 5118 | &PyDateTime_DateType, /* tp_base */ |
| 5119 | 0, /* tp_dict */ |
| 5120 | 0, /* tp_descr_get */ |
| 5121 | 0, /* tp_descr_set */ |
| 5122 | 0, /* tp_dictoffset */ |
| 5123 | 0, /* tp_init */ |
| 5124 | datetime_alloc, /* tp_alloc */ |
| 5125 | datetime_new, /* tp_new */ |
| 5126 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5127 | }; |
| 5128 | |
| 5129 | /* --------------------------------------------------------------------------- |
| 5130 | * Module methods and initialization. |
| 5131 | */ |
| 5132 | |
| 5133 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5135 | }; |
| 5136 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5137 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 5138 | * datetime.h. |
| 5139 | */ |
| 5140 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5141 | &PyDateTime_DateType, |
| 5142 | &PyDateTime_DateTimeType, |
| 5143 | &PyDateTime_TimeType, |
| 5144 | &PyDateTime_DeltaType, |
| 5145 | &PyDateTime_TZInfoType, |
| 5146 | new_date_ex, |
| 5147 | new_datetime_ex, |
| 5148 | new_time_ex, |
| 5149 | new_delta_ex, |
| 5150 | datetime_fromtimestamp, |
| 5151 | date_fromtimestamp |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5152 | }; |
| 5153 | |
| 5154 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5155 | |
| 5156 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5157 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5158 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5159 | "Fast implementation of the datetime type.", |
| 5160 | -1, |
| 5161 | module_methods, |
| 5162 | NULL, |
| 5163 | NULL, |
| 5164 | NULL, |
| 5165 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5166 | }; |
| 5167 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5168 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5169 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | PyObject *m; /* a module object */ |
| 5172 | PyObject *d; /* its dict */ |
| 5173 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5174 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | m = PyModule_Create(&datetimemodule); |
| 5177 | if (m == NULL) |
| 5178 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 5181 | return NULL; |
| 5182 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 5183 | return NULL; |
| 5184 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 5185 | return NULL; |
| 5186 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 5187 | return NULL; |
| 5188 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 5189 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5190 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 5191 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | /* timedelta values */ |
| 5194 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5196 | x = new_delta(0, 0, 1, 0); |
| 5197 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5198 | return NULL; |
| 5199 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5201 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 5202 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5203 | return NULL; |
| 5204 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 5207 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5208 | return NULL; |
| 5209 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5211 | /* date values */ |
| 5212 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5214 | x = new_date(1, 1, 1); |
| 5215 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5216 | return NULL; |
| 5217 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5219 | x = new_date(MAXYEAR, 12, 31); |
| 5220 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5221 | return NULL; |
| 5222 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5224 | x = new_delta(1, 0, 0, 0); |
| 5225 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5226 | return NULL; |
| 5227 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | /* time values */ |
| 5230 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | x = new_time(0, 0, 0, 0, Py_None); |
| 5233 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5234 | return NULL; |
| 5235 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5237 | x = new_time(23, 59, 59, 999999, Py_None); |
| 5238 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5239 | return NULL; |
| 5240 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 | x = new_delta(0, 0, 1, 0); |
| 5243 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5244 | return NULL; |
| 5245 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | /* datetime values */ |
| 5248 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
| 5251 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5252 | return NULL; |
| 5253 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
| 5256 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5257 | return NULL; |
| 5258 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | x = new_delta(0, 0, 1, 0); |
| 5261 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5262 | return NULL; |
| 5263 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5264 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5265 | /* timezone values */ |
| 5266 | d = PyDateTime_TimeZoneType.tp_dict; |
| 5267 | |
| 5268 | delta = new_delta(0, 0, 0, 0); |
| 5269 | if (delta == NULL) |
| 5270 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5271 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5272 | Py_DECREF(delta); |
| 5273 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 5274 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5275 | PyDateTime_TimeZone_UTC = x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5276 | |
| 5277 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 5278 | if (delta == NULL) |
| 5279 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5280 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5281 | Py_DECREF(delta); |
| 5282 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5283 | return NULL; |
| 5284 | Py_DECREF(x); |
| 5285 | |
| 5286 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 5287 | if (delta == NULL) |
| 5288 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5289 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5290 | Py_DECREF(delta); |
| 5291 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5292 | return NULL; |
| 5293 | Py_DECREF(x); |
| 5294 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5295 | /* Epoch */ |
| 5296 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
| 5297 | PyDateTime_TimeZone_UTC); |
| 5298 | if (PyDateTime_Epoch == NULL) |
| 5299 | return NULL; |
| 5300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5302 | PyModule_AddIntMacro(m, MINYEAR); |
| 5303 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5305 | Py_INCREF(&PyDateTime_DateType); |
| 5306 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | Py_INCREF(&PyDateTime_DateTimeType); |
| 5309 | PyModule_AddObject(m, "datetime", |
| 5310 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | Py_INCREF(&PyDateTime_TimeType); |
| 5313 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5315 | Py_INCREF(&PyDateTime_DeltaType); |
| 5316 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5318 | Py_INCREF(&PyDateTime_TZInfoType); |
| 5319 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5320 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5321 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 5322 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 5323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 5325 | if (x == NULL) |
| 5326 | return NULL; |
| 5327 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 5330 | * pasting together 4 single years. |
| 5331 | */ |
| 5332 | assert(DI4Y == 4 * 365 + 1); |
| 5333 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 5336 | * get from pasting together 4 100-year cycles. |
| 5337 | */ |
| 5338 | assert(DI400Y == 4 * DI100Y + 1); |
| 5339 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5341 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 5342 | * pasting together 25 4-year cycles. |
| 5343 | */ |
| 5344 | assert(DI100Y == 25 * DI4Y - 1); |
| 5345 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5347 | us_per_us = PyLong_FromLong(1); |
| 5348 | us_per_ms = PyLong_FromLong(1000); |
| 5349 | us_per_second = PyLong_FromLong(1000000); |
| 5350 | us_per_minute = PyLong_FromLong(60000000); |
| 5351 | seconds_per_day = PyLong_FromLong(24 * 3600); |
| 5352 | if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL || |
| 5353 | us_per_minute == NULL || seconds_per_day == NULL) |
| 5354 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5356 | /* The rest are too big for 32-bit ints, but even |
| 5357 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5358 | */ |
| 5359 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5360 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5361 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5362 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5363 | return NULL; |
| 5364 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5365 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5366 | |
| 5367 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5368 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5369 | x.n = x stripped of its timezone -- its naive time. |
| 5370 | x.o = x.utcoffset(), and assuming that doesn't raise an exception or |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5371 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5372 | x.d = x.dst(), and assuming that doesn't raise an exception or |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5373 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5374 | x.s = x's standard offset, x.o - x.d |
| 5375 | |
| 5376 | Now some derived rules, where k is a duration (timedelta). |
| 5377 | |
| 5378 | 1. x.o = x.s + x.d |
| 5379 | This follows from the definition of x.s. |
| 5380 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5381 | 2. If x and y have the same tzinfo member, x.s = y.s. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5382 | This is actually a requirement, an assumption we need to make about |
| 5383 | sane tzinfo classes. |
| 5384 | |
| 5385 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5386 | This is again a requirement for a sane tzinfo class. |
| 5387 | |
| 5388 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5389 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5390 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5391 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5392 | Again follows from how arithmetic is defined. |
| 5393 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5394 | Now we can explain tz.fromutc(x). Let's assume it's an interesting case |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5395 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5396 | None when called). |
| 5397 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5398 | The function wants to return a datetime y with timezone tz, equivalent to x. |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5399 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5400 | |
| 5401 | By #3, we want |
| 5402 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5403 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5404 | |
| 5405 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5406 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5407 | becomes true; in effect, we want to solve [2] for k: |
| 5408 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5409 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5410 | |
| 5411 | By #1, this is the same as |
| 5412 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5413 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5414 | |
| 5415 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5416 | Substituting that into [3], |
| 5417 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5418 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5419 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5420 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5421 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5422 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5423 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5424 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5425 | very large, since all offset-returning methods return a duration of magnitude |
| 5426 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5427 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5428 | |
| 5429 | In any case, the new value is |
| 5430 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5431 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5432 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5433 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5434 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5435 | |
| 5436 | At this point, if |
| 5437 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5438 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5439 | |
| 5440 | we have an equivalent time, and are almost done. The insecurity here is |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5441 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5442 | time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5443 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5444 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5445 | on the day DST starts. We want to return the 1:MM EST spelling because that's |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5446 | the only spelling that makes sense on the local wall clock. |
| 5447 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5448 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5449 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5450 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5451 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5452 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5453 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5454 | Now |
| 5455 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5456 | (y + y.s).n = by #5 |
| 5457 | y.n + y.s = since y.n = x.n |
| 5458 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5459 | y.s = z.s by #2 |
| 5460 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5461 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5462 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5463 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5464 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5465 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5466 | x.n - x.n - z.s + z.o = cancelling |
| 5467 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5468 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5469 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5470 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5471 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5472 | If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5473 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5474 | if z.d = 0, then we have a UTC equivalent, and are also done. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5475 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5476 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5477 | add to z (in effect, z is in tz's standard time, and we need to shift the |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5478 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5479 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5480 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5481 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5482 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5483 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5484 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5485 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5486 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5487 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5488 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5489 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5490 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5491 | the justifications for the kinds of substitutions we've done several times |
| 5492 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5493 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5494 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5495 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5496 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5497 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5498 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5499 | - z.o + z'.o = #1 twice |
| 5500 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5501 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5502 | |
| 5503 | So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal, |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5504 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5505 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5506 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5507 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 5508 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 5509 | would have to change the result dst() returns: we start in DST, and moving |
| 5510 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5511 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5512 | There isn't a sane case where this can happen. The closest it gets is at |
| 5513 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 5514 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 5515 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 5516 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 5517 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 5518 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 5519 | standard time. Since that's what the local clock *does*, we want to map both |
| 5520 | UTC hours 5:MM and 6:MM to 1:MM Eastern. The result is ambiguous |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5521 | in local time, but so it goes -- it's the way the local clock works. |
| 5522 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5523 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 5524 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 5525 | z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8] |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5526 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 5527 | |
| 5528 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 5529 | we would have stopped then), and we know z.d != z'.d (else [8] would have held |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 5530 | and we would have stopped then), and there are only 2 possible values dst() can |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5531 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 5532 | but the reasoning doesn't depend on the example -- it depends on there being |
| 5533 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5534 | z' must be in standard time, and is the spelling we want in this case. |
| 5535 | |
| 5536 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 5537 | concerned (because it takes z' as being in standard time rather than the |
| 5538 | daylight time we intend here), but returning it gives the real-life "local |
| 5539 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 5540 | tz. |
| 5541 | |
| 5542 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 5543 | the 1:MM standard time spelling we want. |
| 5544 | |
| 5545 | So how can this break? One of the assumptions must be violated. Two |
| 5546 | possibilities: |
| 5547 | |
| 5548 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 5549 | time zone. This isn't true if, for political reasons or continental drift, |
| 5550 | a region decides to change its base offset from UTC. |
| 5551 | |
| 5552 | 2) There may be versions of "double daylight" time where the tail end of |
| 5553 | the analysis gives up a step too early. I haven't thought about that |
| 5554 | enough to say. |
| 5555 | |
| 5556 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 5557 | "almost all" time zones: so long as the standard offset is invariant, it |
| 5558 | doesn't matter if daylight time transition points change from year to year, or |
| 5559 | if daylight time is skipped in some years; it doesn't matter how large or |
| 5560 | small dst() may get within its bounds; and it doesn't even matter if some |
| 5561 | perverse time zone returns a negative dst()). So a breaking case must be |
| 5562 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5563 | --------------------------------------------------------------------------- */ |