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