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