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