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