Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | /* C implementation for the date/time type documented at |
| 2 | * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage |
| 3 | */ |
| 4 | |
| 5 | #include "Python.h" |
| 6 | #include "modsupport.h" |
| 7 | #include "structmember.h" |
| 8 | |
| 9 | #include <time.h> |
| 10 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 11 | #include "timefuncs.h" |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 12 | |
| 13 | /* Differentiate between building the core module and building extension |
| 14 | * modules. |
| 15 | */ |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 16 | #ifndef Py_BUILD_CORE |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 17 | #define Py_BUILD_CORE |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 18 | #endif |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 19 | #include "datetime.h" |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 20 | #undef Py_BUILD_CORE |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 21 | |
| 22 | /* We require that C int be at least 32 bits, and use int virtually |
| 23 | * everywhere. In just a few cases we use a temp long, where a Python |
| 24 | * API returns a C long. In such cases, we have to ensure that the |
| 25 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 26 | */ |
| 27 | #if SIZEOF_INT < 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 28 | # error "datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #define MINYEAR 1 |
| 32 | #define MAXYEAR 9999 |
| 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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 128 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 201 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 551 | int result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 603 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 618 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 653 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 695 | PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 727 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 729 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 777 | PyObject *result; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 797 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 817 | int *none) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 890 | PyObject *result; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 936 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 996 | int none; |
| 997 | PyObject *tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1053 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1106 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1114 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1198 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1199 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1437 | PyObject *result; |
| 1438 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1440 | 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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1669 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1671 | PyObject *pyus_left; |
| 1672 | PyObject *pyus_right; |
| 1673 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1675 | pyus_left = delta_to_microseconds(left); |
| 1676 | if (pyus_left == NULL) |
| 1677 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1679 | pyus_right = delta_to_microseconds(right); |
| 1680 | if (pyus_right == NULL) { |
| 1681 | Py_DECREF(pyus_left); |
| 1682 | return NULL; |
| 1683 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1685 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1686 | Py_DECREF(pyus_left); |
| 1687 | Py_DECREF(pyus_right); |
| 1688 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | static PyObject * |
| 1692 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1694 | PyObject *pyus_left; |
| 1695 | PyObject *pyus_right; |
| 1696 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1698 | pyus_left = delta_to_microseconds(left); |
| 1699 | if (pyus_left == NULL) |
| 1700 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1702 | pyus_right = delta_to_microseconds(right); |
| 1703 | if (pyus_right == NULL) { |
| 1704 | Py_DECREF(pyus_left); |
| 1705 | return NULL; |
| 1706 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1708 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1709 | Py_DECREF(pyus_left); |
| 1710 | Py_DECREF(pyus_right); |
| 1711 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1715 | delta_add(PyObject *left, PyObject *right) |
| 1716 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1717 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1719 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1720 | /* delta + delta */ |
| 1721 | /* The C-level additions can't overflow because of the |
| 1722 | * invariant bounds. |
| 1723 | */ |
| 1724 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1725 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1726 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1727 | GET_TD_MICROSECONDS(right); |
| 1728 | result = new_delta(days, seconds, microseconds, 1); |
| 1729 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1731 | if (result == Py_NotImplemented) |
| 1732 | Py_INCREF(result); |
| 1733 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | static PyObject * |
| 1737 | delta_negative(PyDateTime_Delta *self) |
| 1738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1739 | return new_delta(-GET_TD_DAYS(self), |
| 1740 | -GET_TD_SECONDS(self), |
| 1741 | -GET_TD_MICROSECONDS(self), |
| 1742 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static PyObject * |
| 1746 | delta_positive(PyDateTime_Delta *self) |
| 1747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1748 | /* Could optimize this (by returning self) if this isn't a |
| 1749 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1750 | */ |
| 1751 | return new_delta(GET_TD_DAYS(self), |
| 1752 | GET_TD_SECONDS(self), |
| 1753 | GET_TD_MICROSECONDS(self), |
| 1754 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static PyObject * |
| 1758 | delta_abs(PyDateTime_Delta *self) |
| 1759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1760 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1762 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1763 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1765 | if (GET_TD_DAYS(self) < 0) |
| 1766 | result = delta_negative(self); |
| 1767 | else |
| 1768 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1770 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static PyObject * |
| 1774 | delta_subtract(PyObject *left, PyObject *right) |
| 1775 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1776 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1778 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1779 | /* delta - delta */ |
| 1780 | PyObject *minus_right = PyNumber_Negative(right); |
| 1781 | if (minus_right) { |
| 1782 | result = delta_add(left, minus_right); |
| 1783 | Py_DECREF(minus_right); |
| 1784 | } |
| 1785 | else |
| 1786 | result = NULL; |
| 1787 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1789 | if (result == Py_NotImplemented) |
| 1790 | Py_INCREF(result); |
| 1791 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1794 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1795 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1797 | if (PyDelta_Check(other)) { |
| 1798 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 1799 | if (diff == 0) { |
| 1800 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1801 | if (diff == 0) |
| 1802 | diff = GET_TD_MICROSECONDS(self) - |
| 1803 | GET_TD_MICROSECONDS(other); |
| 1804 | } |
| 1805 | return diff_to_bool(diff, op); |
| 1806 | } |
| 1807 | else { |
| 1808 | Py_INCREF(Py_NotImplemented); |
| 1809 | return Py_NotImplemented; |
| 1810 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1814 | |
| 1815 | static long |
| 1816 | delta_hash(PyDateTime_Delta *self) |
| 1817 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1818 | if (self->hashcode == -1) { |
| 1819 | PyObject *temp = delta_getstate(self); |
| 1820 | if (temp != NULL) { |
| 1821 | self->hashcode = PyObject_Hash(temp); |
| 1822 | Py_DECREF(temp); |
| 1823 | } |
| 1824 | } |
| 1825 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | static PyObject * |
| 1829 | delta_multiply(PyObject *left, PyObject *right) |
| 1830 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1831 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1833 | if (PyDelta_Check(left)) { |
| 1834 | /* delta * ??? */ |
| 1835 | if (PyLong_Check(right)) |
| 1836 | result = multiply_int_timedelta(right, |
| 1837 | (PyDateTime_Delta *) left); |
| 1838 | } |
| 1839 | else if (PyLong_Check(left)) |
| 1840 | result = multiply_int_timedelta(left, |
| 1841 | (PyDateTime_Delta *) right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1843 | if (result == Py_NotImplemented) |
| 1844 | Py_INCREF(result); |
| 1845 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | static PyObject * |
| 1849 | delta_divide(PyObject *left, PyObject *right) |
| 1850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1851 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1853 | if (PyDelta_Check(left)) { |
| 1854 | /* delta * ??? */ |
| 1855 | if (PyLong_Check(right)) |
| 1856 | result = divide_timedelta_int( |
| 1857 | (PyDateTime_Delta *)left, |
| 1858 | right); |
| 1859 | else if (PyDelta_Check(right)) |
| 1860 | result = divide_timedelta_timedelta( |
| 1861 | (PyDateTime_Delta *)left, |
| 1862 | (PyDateTime_Delta *)right); |
| 1863 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1865 | if (result == Py_NotImplemented) |
| 1866 | Py_INCREF(result); |
| 1867 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1870 | static PyObject * |
| 1871 | delta_truedivide(PyObject *left, PyObject *right) |
| 1872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1873 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1875 | if (PyDelta_Check(left)) { |
| 1876 | if (PyDelta_Check(right)) |
| 1877 | result = truedivide_timedelta_timedelta( |
| 1878 | (PyDateTime_Delta *)left, |
| 1879 | (PyDateTime_Delta *)right); |
| 1880 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1882 | if (result == Py_NotImplemented) |
| 1883 | Py_INCREF(result); |
| 1884 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | static PyObject * |
| 1888 | delta_remainder(PyObject *left, PyObject *right) |
| 1889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1890 | PyObject *pyus_left; |
| 1891 | PyObject *pyus_right; |
| 1892 | PyObject *pyus_remainder; |
| 1893 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1895 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) { |
| 1896 | Py_INCREF(Py_NotImplemented); |
| 1897 | return Py_NotImplemented; |
| 1898 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1900 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1901 | if (pyus_left == NULL) |
| 1902 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1904 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1905 | if (pyus_right == NULL) { |
| 1906 | Py_DECREF(pyus_left); |
| 1907 | return NULL; |
| 1908 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1910 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 1911 | Py_DECREF(pyus_left); |
| 1912 | Py_DECREF(pyus_right); |
| 1913 | if (pyus_remainder == NULL) |
| 1914 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1916 | remainder = microseconds_to_delta(pyus_remainder); |
| 1917 | Py_DECREF(pyus_remainder); |
| 1918 | if (remainder == NULL) |
| 1919 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1921 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | static PyObject * |
| 1925 | delta_divmod(PyObject *left, PyObject *right) |
| 1926 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1927 | PyObject *pyus_left; |
| 1928 | PyObject *pyus_right; |
| 1929 | PyObject *divmod; |
| 1930 | PyObject *delta; |
| 1931 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1933 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) { |
| 1934 | Py_INCREF(Py_NotImplemented); |
| 1935 | return Py_NotImplemented; |
| 1936 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1938 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1939 | if (pyus_left == NULL) |
| 1940 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1942 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1943 | if (pyus_right == NULL) { |
| 1944 | Py_DECREF(pyus_left); |
| 1945 | return NULL; |
| 1946 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1948 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 1949 | Py_DECREF(pyus_left); |
| 1950 | Py_DECREF(pyus_right); |
| 1951 | if (divmod == NULL) |
| 1952 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1954 | assert(PyTuple_Size(divmod) == 2); |
| 1955 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 1956 | if (delta == NULL) { |
| 1957 | Py_DECREF(divmod); |
| 1958 | return NULL; |
| 1959 | } |
| 1960 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 1961 | Py_DECREF(delta); |
| 1962 | Py_DECREF(divmod); |
| 1963 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1966 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 1967 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 1968 | * so far, and there are factor microseconds per current unit, the number |
| 1969 | * of which is given by num. num * factor is added to sofar in a |
| 1970 | * numerically careful way, and that's the result. Any fractional |
| 1971 | * microseconds left over (this can happen if num is a float type) are |
| 1972 | * added into *leftover. |
| 1973 | * Note that there are many ways this can give an error (NULL) return. |
| 1974 | */ |
| 1975 | static PyObject * |
| 1976 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 1977 | double *leftover) |
| 1978 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1979 | PyObject *prod; |
| 1980 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1982 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1984 | if (PyLong_Check(num)) { |
| 1985 | prod = PyNumber_Multiply(num, factor); |
| 1986 | if (prod == NULL) |
| 1987 | return NULL; |
| 1988 | sum = PyNumber_Add(sofar, prod); |
| 1989 | Py_DECREF(prod); |
| 1990 | return sum; |
| 1991 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 1993 | if (PyFloat_Check(num)) { |
| 1994 | double dnum; |
| 1995 | double fracpart; |
| 1996 | double intpart; |
| 1997 | PyObject *x; |
| 1998 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2000 | /* The Plan: decompose num into an integer part and a |
| 2001 | * fractional part, num = intpart + fracpart. |
| 2002 | * Then num * factor == |
| 2003 | * intpart * factor + fracpart * factor |
| 2004 | * and the LHS can be computed exactly in long arithmetic. |
| 2005 | * The RHS is again broken into an int part and frac part. |
| 2006 | * and the frac part is added into *leftover. |
| 2007 | */ |
| 2008 | dnum = PyFloat_AsDouble(num); |
| 2009 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2010 | return NULL; |
| 2011 | fracpart = modf(dnum, &intpart); |
| 2012 | x = PyLong_FromDouble(intpart); |
| 2013 | if (x == NULL) |
| 2014 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2016 | prod = PyNumber_Multiply(x, factor); |
| 2017 | Py_DECREF(x); |
| 2018 | if (prod == NULL) |
| 2019 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2021 | sum = PyNumber_Add(sofar, prod); |
| 2022 | Py_DECREF(prod); |
| 2023 | if (sum == NULL) |
| 2024 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2026 | if (fracpart == 0.0) |
| 2027 | return sum; |
| 2028 | /* So far we've lost no information. Dealing with the |
| 2029 | * fractional part requires float arithmetic, and may |
| 2030 | * lose a little info. |
| 2031 | */ |
| 2032 | assert(PyLong_Check(factor)); |
| 2033 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2035 | dnum *= fracpart; |
| 2036 | fracpart = modf(dnum, &intpart); |
| 2037 | x = PyLong_FromDouble(intpart); |
| 2038 | if (x == NULL) { |
| 2039 | Py_DECREF(sum); |
| 2040 | return NULL; |
| 2041 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2043 | y = PyNumber_Add(sum, x); |
| 2044 | Py_DECREF(sum); |
| 2045 | Py_DECREF(x); |
| 2046 | *leftover += fracpart; |
| 2047 | return y; |
| 2048 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2050 | PyErr_Format(PyExc_TypeError, |
| 2051 | "unsupported type for timedelta %s component: %s", |
| 2052 | tag, Py_TYPE(num)->tp_name); |
| 2053 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static PyObject * |
| 2057 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2058 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2059 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2061 | /* Argument objects. */ |
| 2062 | PyObject *day = NULL; |
| 2063 | PyObject *second = NULL; |
| 2064 | PyObject *us = NULL; |
| 2065 | PyObject *ms = NULL; |
| 2066 | PyObject *minute = NULL; |
| 2067 | PyObject *hour = NULL; |
| 2068 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2070 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2071 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2072 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2074 | static char *keywords[] = { |
| 2075 | "days", "seconds", "microseconds", "milliseconds", |
| 2076 | "minutes", "hours", "weeks", NULL |
| 2077 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2079 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2080 | keywords, |
| 2081 | &day, &second, &us, |
| 2082 | &ms, &minute, &hour, &week) == 0) |
| 2083 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2085 | x = PyLong_FromLong(0); |
| 2086 | if (x == NULL) |
| 2087 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2089 | #define CLEANUP \ |
| 2090 | Py_DECREF(x); \ |
| 2091 | x = y; \ |
| 2092 | if (x == NULL) \ |
| 2093 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2095 | if (us) { |
| 2096 | y = accum("microseconds", x, us, us_per_us, &leftover_us); |
| 2097 | CLEANUP; |
| 2098 | } |
| 2099 | if (ms) { |
| 2100 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2101 | CLEANUP; |
| 2102 | } |
| 2103 | if (second) { |
| 2104 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2105 | CLEANUP; |
| 2106 | } |
| 2107 | if (minute) { |
| 2108 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2109 | CLEANUP; |
| 2110 | } |
| 2111 | if (hour) { |
| 2112 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2113 | CLEANUP; |
| 2114 | } |
| 2115 | if (day) { |
| 2116 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2117 | CLEANUP; |
| 2118 | } |
| 2119 | if (week) { |
| 2120 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2121 | CLEANUP; |
| 2122 | } |
| 2123 | if (leftover_us) { |
| 2124 | /* Round to nearest whole # of us, and add into x. */ |
| 2125 | PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); |
| 2126 | if (temp == NULL) { |
| 2127 | Py_DECREF(x); |
| 2128 | goto Done; |
| 2129 | } |
| 2130 | y = PyNumber_Add(x, temp); |
| 2131 | Py_DECREF(temp); |
| 2132 | CLEANUP; |
| 2133 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2135 | self = microseconds_to_delta_ex(x, type); |
| 2136 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2137 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2138 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2139 | |
| 2140 | #undef CLEANUP |
| 2141 | } |
| 2142 | |
| 2143 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2144 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2146 | return (GET_TD_DAYS(self) != 0 |
| 2147 | || GET_TD_SECONDS(self) != 0 |
| 2148 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | static PyObject * |
| 2152 | delta_repr(PyDateTime_Delta *self) |
| 2153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2154 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2155 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2156 | Py_TYPE(self)->tp_name, |
| 2157 | GET_TD_DAYS(self), |
| 2158 | GET_TD_SECONDS(self), |
| 2159 | GET_TD_MICROSECONDS(self)); |
| 2160 | if (GET_TD_SECONDS(self) != 0) |
| 2161 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2162 | Py_TYPE(self)->tp_name, |
| 2163 | GET_TD_DAYS(self), |
| 2164 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2166 | return PyUnicode_FromFormat("%s(%d)", |
| 2167 | Py_TYPE(self)->tp_name, |
| 2168 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | static PyObject * |
| 2172 | delta_str(PyDateTime_Delta *self) |
| 2173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2174 | int us = GET_TD_MICROSECONDS(self); |
| 2175 | int seconds = GET_TD_SECONDS(self); |
| 2176 | int minutes = divmod(seconds, 60, &seconds); |
| 2177 | int hours = divmod(minutes, 60, &minutes); |
| 2178 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2180 | if (days) { |
| 2181 | if (us) |
| 2182 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2183 | days, (days == 1 || days == -1) ? "" : "s", |
| 2184 | hours, minutes, seconds, us); |
| 2185 | else |
| 2186 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2187 | days, (days == 1 || days == -1) ? "" : "s", |
| 2188 | hours, minutes, seconds); |
| 2189 | } else { |
| 2190 | if (us) |
| 2191 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2192 | hours, minutes, seconds, us); |
| 2193 | else |
| 2194 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2195 | hours, minutes, seconds); |
| 2196 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2197 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2200 | /* Pickle support, a simple use of __reduce__. */ |
| 2201 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2202 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2203 | static PyObject * |
| 2204 | delta_getstate(PyDateTime_Delta *self) |
| 2205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2206 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2207 | GET_TD_SECONDS(self), |
| 2208 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2211 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2212 | delta_total_seconds(PyObject *self) |
| 2213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2214 | PyObject *total_seconds; |
| 2215 | PyObject *total_microseconds; |
| 2216 | PyObject *one_million; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2218 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2219 | if (total_microseconds == NULL) |
| 2220 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2222 | one_million = PyLong_FromLong(1000000L); |
| 2223 | if (one_million == NULL) { |
| 2224 | Py_DECREF(total_microseconds); |
| 2225 | return NULL; |
| 2226 | } |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2228 | total_seconds = PyNumber_TrueDivide(total_microseconds, one_million); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2230 | Py_DECREF(total_microseconds); |
| 2231 | Py_DECREF(one_million); |
| 2232 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2236 | delta_reduce(PyDateTime_Delta* self) |
| 2237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2238 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2242 | |
| 2243 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2245 | {"days", T_INT, OFFSET(days), READONLY, |
| 2246 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2248 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2249 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2251 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2252 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2253 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2254 | }; |
| 2255 | |
| 2256 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2257 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2258 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2260 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2261 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2263 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2264 | }; |
| 2265 | |
| 2266 | static char delta_doc[] = |
| 2267 | PyDoc_STR("Difference between two datetime values."); |
| 2268 | |
| 2269 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2270 | delta_add, /* nb_add */ |
| 2271 | delta_subtract, /* nb_subtract */ |
| 2272 | delta_multiply, /* nb_multiply */ |
| 2273 | delta_remainder, /* nb_remainder */ |
| 2274 | delta_divmod, /* nb_divmod */ |
| 2275 | 0, /* nb_power */ |
| 2276 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2277 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2278 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2279 | (inquiry)delta_bool, /* nb_bool */ |
| 2280 | 0, /*nb_invert*/ |
| 2281 | 0, /*nb_lshift*/ |
| 2282 | 0, /*nb_rshift*/ |
| 2283 | 0, /*nb_and*/ |
| 2284 | 0, /*nb_xor*/ |
| 2285 | 0, /*nb_or*/ |
| 2286 | 0, /*nb_int*/ |
| 2287 | 0, /*nb_reserved*/ |
| 2288 | 0, /*nb_float*/ |
| 2289 | 0, /*nb_inplace_add*/ |
| 2290 | 0, /*nb_inplace_subtract*/ |
| 2291 | 0, /*nb_inplace_multiply*/ |
| 2292 | 0, /*nb_inplace_remainder*/ |
| 2293 | 0, /*nb_inplace_power*/ |
| 2294 | 0, /*nb_inplace_lshift*/ |
| 2295 | 0, /*nb_inplace_rshift*/ |
| 2296 | 0, /*nb_inplace_and*/ |
| 2297 | 0, /*nb_inplace_xor*/ |
| 2298 | 0, /*nb_inplace_or*/ |
| 2299 | delta_divide, /* nb_floor_divide */ |
| 2300 | delta_truedivide, /* nb_true_divide */ |
| 2301 | 0, /* nb_inplace_floor_divide */ |
| 2302 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2303 | }; |
| 2304 | |
| 2305 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2306 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2307 | "datetime.timedelta", /* tp_name */ |
| 2308 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2309 | 0, /* tp_itemsize */ |
| 2310 | 0, /* tp_dealloc */ |
| 2311 | 0, /* tp_print */ |
| 2312 | 0, /* tp_getattr */ |
| 2313 | 0, /* tp_setattr */ |
| 2314 | 0, /* tp_reserved */ |
| 2315 | (reprfunc)delta_repr, /* tp_repr */ |
| 2316 | &delta_as_number, /* tp_as_number */ |
| 2317 | 0, /* tp_as_sequence */ |
| 2318 | 0, /* tp_as_mapping */ |
| 2319 | (hashfunc)delta_hash, /* tp_hash */ |
| 2320 | 0, /* tp_call */ |
| 2321 | (reprfunc)delta_str, /* tp_str */ |
| 2322 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2323 | 0, /* tp_setattro */ |
| 2324 | 0, /* tp_as_buffer */ |
| 2325 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2326 | delta_doc, /* tp_doc */ |
| 2327 | 0, /* tp_traverse */ |
| 2328 | 0, /* tp_clear */ |
| 2329 | delta_richcompare, /* tp_richcompare */ |
| 2330 | 0, /* tp_weaklistoffset */ |
| 2331 | 0, /* tp_iter */ |
| 2332 | 0, /* tp_iternext */ |
| 2333 | delta_methods, /* tp_methods */ |
| 2334 | delta_members, /* tp_members */ |
| 2335 | 0, /* tp_getset */ |
| 2336 | 0, /* tp_base */ |
| 2337 | 0, /* tp_dict */ |
| 2338 | 0, /* tp_descr_get */ |
| 2339 | 0, /* tp_descr_set */ |
| 2340 | 0, /* tp_dictoffset */ |
| 2341 | 0, /* tp_init */ |
| 2342 | 0, /* tp_alloc */ |
| 2343 | delta_new, /* tp_new */ |
| 2344 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2345 | }; |
| 2346 | |
| 2347 | /* |
| 2348 | * PyDateTime_Date implementation. |
| 2349 | */ |
| 2350 | |
| 2351 | /* Accessor properties. */ |
| 2352 | |
| 2353 | static PyObject * |
| 2354 | date_year(PyDateTime_Date *self, void *unused) |
| 2355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2356 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | static PyObject * |
| 2360 | date_month(PyDateTime_Date *self, void *unused) |
| 2361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2362 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | static PyObject * |
| 2366 | date_day(PyDateTime_Date *self, void *unused) |
| 2367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2368 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2372 | {"year", (getter)date_year}, |
| 2373 | {"month", (getter)date_month}, |
| 2374 | {"day", (getter)date_day}, |
| 2375 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2376 | }; |
| 2377 | |
| 2378 | /* Constructors. */ |
| 2379 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2380 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2381 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2382 | static PyObject * |
| 2383 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2385 | PyObject *self = NULL; |
| 2386 | PyObject *state; |
| 2387 | int year; |
| 2388 | int month; |
| 2389 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2391 | /* Check for invocation from pickle with __getstate__ state */ |
| 2392 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2393 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2394 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2395 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2396 | { |
| 2397 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2399 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2400 | if (me != NULL) { |
| 2401 | char *pdata = PyBytes_AS_STRING(state); |
| 2402 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2403 | me->hashcode = -1; |
| 2404 | } |
| 2405 | return (PyObject *)me; |
| 2406 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2408 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2409 | &year, &month, &day)) { |
| 2410 | if (check_date_args(year, month, day) < 0) |
| 2411 | return NULL; |
| 2412 | self = new_date_ex(year, month, day, type); |
| 2413 | } |
| 2414 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | /* Return new date from localtime(t). */ |
| 2418 | static PyObject * |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 2419 | date_local_from_time_t(PyObject *cls, double ts) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2421 | struct tm *tm; |
| 2422 | time_t t; |
| 2423 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2425 | t = _PyTime_DoubleToTimet(ts); |
| 2426 | if (t == (time_t)-1 && PyErr_Occurred()) |
| 2427 | return NULL; |
| 2428 | tm = localtime(&t); |
| 2429 | if (tm) |
| 2430 | result = PyObject_CallFunction(cls, "iii", |
| 2431 | tm->tm_year + 1900, |
| 2432 | tm->tm_mon + 1, |
| 2433 | tm->tm_mday); |
| 2434 | else |
| 2435 | PyErr_SetString(PyExc_ValueError, |
| 2436 | "timestamp out of range for " |
| 2437 | "platform localtime() function"); |
| 2438 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /* Return new date from current time. |
| 2442 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2443 | * only way to be sure of that is to *call* time.time(). That's not |
| 2444 | * generally the same as calling C's time. |
| 2445 | */ |
| 2446 | static PyObject * |
| 2447 | date_today(PyObject *cls, PyObject *dummy) |
| 2448 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2449 | PyObject *time; |
| 2450 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2452 | time = time_time(); |
| 2453 | if (time == NULL) |
| 2454 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2456 | /* Note well: today() is a class method, so this may not call |
| 2457 | * date.fromtimestamp. For example, it may call |
| 2458 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2459 | * time.time() delivers; if someone were gonzo about optimization, |
| 2460 | * date.today() could get away with plain C time(). |
| 2461 | */ |
| 2462 | result = PyObject_CallMethod(cls, "fromtimestamp", "O", time); |
| 2463 | Py_DECREF(time); |
| 2464 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2468 | static PyObject * |
| 2469 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2471 | double timestamp; |
| 2472 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2474 | if (PyArg_ParseTuple(args, "d:fromtimestamp", ×tamp)) |
| 2475 | result = date_local_from_time_t(cls, timestamp); |
| 2476 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2480 | * the ordinal is out of range. |
| 2481 | */ |
| 2482 | static PyObject * |
| 2483 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2485 | PyObject *result = NULL; |
| 2486 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2488 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2489 | int year; |
| 2490 | int month; |
| 2491 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2493 | if (ordinal < 1) |
| 2494 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2495 | ">= 1"); |
| 2496 | else { |
| 2497 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2498 | result = PyObject_CallFunction(cls, "iii", |
| 2499 | year, month, day); |
| 2500 | } |
| 2501 | } |
| 2502 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | /* |
| 2506 | * Date arithmetic. |
| 2507 | */ |
| 2508 | |
| 2509 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2510 | * instead. |
| 2511 | */ |
| 2512 | static PyObject * |
| 2513 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2514 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2515 | PyObject *result = NULL; |
| 2516 | int year = GET_YEAR(date); |
| 2517 | int month = GET_MONTH(date); |
| 2518 | int deltadays = GET_TD_DAYS(delta); |
| 2519 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2520 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2522 | if (normalize_date(&year, &month, &day) >= 0) |
| 2523 | result = new_date(year, month, day); |
| 2524 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | static PyObject * |
| 2528 | date_add(PyObject *left, PyObject *right) |
| 2529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2530 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) { |
| 2531 | Py_INCREF(Py_NotImplemented); |
| 2532 | return Py_NotImplemented; |
| 2533 | } |
| 2534 | if (PyDate_Check(left)) { |
| 2535 | /* date + ??? */ |
| 2536 | if (PyDelta_Check(right)) |
| 2537 | /* date + delta */ |
| 2538 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2539 | (PyDateTime_Delta *) right, |
| 2540 | 0); |
| 2541 | } |
| 2542 | else { |
| 2543 | /* ??? + date |
| 2544 | * 'right' must be one of us, or we wouldn't have been called |
| 2545 | */ |
| 2546 | if (PyDelta_Check(left)) |
| 2547 | /* delta + date */ |
| 2548 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2549 | (PyDateTime_Delta *) left, |
| 2550 | 0); |
| 2551 | } |
| 2552 | Py_INCREF(Py_NotImplemented); |
| 2553 | return Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | static PyObject * |
| 2557 | date_subtract(PyObject *left, PyObject *right) |
| 2558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2559 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) { |
| 2560 | Py_INCREF(Py_NotImplemented); |
| 2561 | return Py_NotImplemented; |
| 2562 | } |
| 2563 | if (PyDate_Check(left)) { |
| 2564 | if (PyDate_Check(right)) { |
| 2565 | /* date - date */ |
| 2566 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2567 | GET_MONTH(left), |
| 2568 | GET_DAY(left)); |
| 2569 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2570 | GET_MONTH(right), |
| 2571 | GET_DAY(right)); |
| 2572 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2573 | } |
| 2574 | if (PyDelta_Check(right)) { |
| 2575 | /* date - delta */ |
| 2576 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2577 | (PyDateTime_Delta *) right, |
| 2578 | 1); |
| 2579 | } |
| 2580 | } |
| 2581 | Py_INCREF(Py_NotImplemented); |
| 2582 | return Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | |
| 2586 | /* Various ways to turn a date into a string. */ |
| 2587 | |
| 2588 | static PyObject * |
| 2589 | date_repr(PyDateTime_Date *self) |
| 2590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2591 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2592 | Py_TYPE(self)->tp_name, |
| 2593 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | static PyObject * |
| 2597 | date_isoformat(PyDateTime_Date *self) |
| 2598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2599 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2600 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2603 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2604 | static PyObject * |
| 2605 | date_str(PyDateTime_Date *self) |
| 2606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2607 | return PyObject_CallMethod((PyObject *)self, "isoformat", "()"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | |
| 2611 | static PyObject * |
| 2612 | date_ctime(PyDateTime_Date *self) |
| 2613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2614 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | static PyObject * |
| 2618 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2620 | /* This method can be inherited, and needs to call the |
| 2621 | * timetuple() method appropriate to self's class. |
| 2622 | */ |
| 2623 | PyObject *result; |
| 2624 | PyObject *tuple; |
| 2625 | PyObject *format; |
| 2626 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2628 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2629 | &format)) |
| 2630 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2632 | tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()"); |
| 2633 | if (tuple == NULL) |
| 2634 | return NULL; |
| 2635 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2636 | (PyObject *)self); |
| 2637 | Py_DECREF(tuple); |
| 2638 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2641 | static PyObject * |
| 2642 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2644 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2646 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2647 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2649 | /* if the format is zero length, return str(self) */ |
| 2650 | if (PyUnicode_GetSize(format) == 0) |
| 2651 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2653 | return PyObject_CallMethod((PyObject *)self, "strftime", "O", format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2656 | /* ISO methods. */ |
| 2657 | |
| 2658 | static PyObject * |
| 2659 | date_isoweekday(PyDateTime_Date *self) |
| 2660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2661 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2663 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | static PyObject * |
| 2667 | date_isocalendar(PyDateTime_Date *self) |
| 2668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2669 | int year = GET_YEAR(self); |
| 2670 | int week1_monday = iso_week1_monday(year); |
| 2671 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2672 | int week; |
| 2673 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2675 | week = divmod(today - week1_monday, 7, &day); |
| 2676 | if (week < 0) { |
| 2677 | --year; |
| 2678 | week1_monday = iso_week1_monday(year); |
| 2679 | week = divmod(today - week1_monday, 7, &day); |
| 2680 | } |
| 2681 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2682 | ++year; |
| 2683 | week = 0; |
| 2684 | } |
| 2685 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | /* Miscellaneous methods. */ |
| 2689 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2690 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2691 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2693 | if (PyDate_Check(other)) { |
| 2694 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2695 | ((PyDateTime_Date *)other)->data, |
| 2696 | _PyDateTime_DATE_DATASIZE); |
| 2697 | return diff_to_bool(diff, op); |
| 2698 | } |
| 2699 | else { |
| 2700 | Py_INCREF(Py_NotImplemented); |
| 2701 | return Py_NotImplemented; |
| 2702 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | static PyObject * |
| 2706 | date_timetuple(PyDateTime_Date *self) |
| 2707 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2708 | return build_struct_time(GET_YEAR(self), |
| 2709 | GET_MONTH(self), |
| 2710 | GET_DAY(self), |
| 2711 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2714 | static PyObject * |
| 2715 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2716 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2717 | PyObject *clone; |
| 2718 | PyObject *tuple; |
| 2719 | int year = GET_YEAR(self); |
| 2720 | int month = GET_MONTH(self); |
| 2721 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2723 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2724 | &year, &month, &day)) |
| 2725 | return NULL; |
| 2726 | tuple = Py_BuildValue("iii", year, month, day); |
| 2727 | if (tuple == NULL) |
| 2728 | return NULL; |
| 2729 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2730 | Py_DECREF(tuple); |
| 2731 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2734 | /* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2735 | Borrowed from stringobject.c, originally it was string_hash() |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2736 | */ |
| 2737 | static long |
| 2738 | generic_hash(unsigned char *data, int len) |
| 2739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2740 | register unsigned char *p; |
| 2741 | register long x; |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2743 | p = (unsigned char *) data; |
| 2744 | x = *p << 7; |
| 2745 | while (--len >= 0) |
| 2746 | x = (1000003*x) ^ *p++; |
| 2747 | x ^= len; |
| 2748 | if (x == -1) |
| 2749 | x = -2; |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2751 | return x; |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | |
| 2755 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2756 | |
| 2757 | static long |
| 2758 | date_hash(PyDateTime_Date *self) |
| 2759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2760 | if (self->hashcode == -1) |
| 2761 | self->hashcode = generic_hash( |
| 2762 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2764 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | static PyObject * |
| 2768 | date_toordinal(PyDateTime_Date *self) |
| 2769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2770 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2771 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | static PyObject * |
| 2775 | date_weekday(PyDateTime_Date *self) |
| 2776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2777 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2779 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2782 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2783 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2784 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2785 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2786 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2788 | PyObject* field; |
| 2789 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2790 | _PyDateTime_DATE_DATASIZE); |
| 2791 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2795 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2797 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2802 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2804 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2805 | METH_CLASS, |
| 2806 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2807 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2809 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2810 | METH_CLASS, |
| 2811 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2812 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2814 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2815 | PyDoc_STR("Current date or datetime: same as " |
| 2816 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2818 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2820 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2821 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2823 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2824 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2826 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2827 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2829 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2830 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2832 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2833 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2834 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2836 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2837 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2839 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2840 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2841 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2843 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2844 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2845 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2847 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2848 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2849 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2851 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2852 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2854 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2855 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2857 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2858 | }; |
| 2859 | |
| 2860 | static char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2861 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2862 | |
| 2863 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2864 | date_add, /* nb_add */ |
| 2865 | date_subtract, /* nb_subtract */ |
| 2866 | 0, /* nb_multiply */ |
| 2867 | 0, /* nb_remainder */ |
| 2868 | 0, /* nb_divmod */ |
| 2869 | 0, /* nb_power */ |
| 2870 | 0, /* nb_negative */ |
| 2871 | 0, /* nb_positive */ |
| 2872 | 0, /* nb_absolute */ |
| 2873 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2874 | }; |
| 2875 | |
| 2876 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2877 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2878 | "datetime.date", /* tp_name */ |
| 2879 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2880 | 0, /* tp_itemsize */ |
| 2881 | 0, /* tp_dealloc */ |
| 2882 | 0, /* tp_print */ |
| 2883 | 0, /* tp_getattr */ |
| 2884 | 0, /* tp_setattr */ |
| 2885 | 0, /* tp_reserved */ |
| 2886 | (reprfunc)date_repr, /* tp_repr */ |
| 2887 | &date_as_number, /* tp_as_number */ |
| 2888 | 0, /* tp_as_sequence */ |
| 2889 | 0, /* tp_as_mapping */ |
| 2890 | (hashfunc)date_hash, /* tp_hash */ |
| 2891 | 0, /* tp_call */ |
| 2892 | (reprfunc)date_str, /* tp_str */ |
| 2893 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2894 | 0, /* tp_setattro */ |
| 2895 | 0, /* tp_as_buffer */ |
| 2896 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2897 | date_doc, /* tp_doc */ |
| 2898 | 0, /* tp_traverse */ |
| 2899 | 0, /* tp_clear */ |
| 2900 | date_richcompare, /* tp_richcompare */ |
| 2901 | 0, /* tp_weaklistoffset */ |
| 2902 | 0, /* tp_iter */ |
| 2903 | 0, /* tp_iternext */ |
| 2904 | date_methods, /* tp_methods */ |
| 2905 | 0, /* tp_members */ |
| 2906 | date_getset, /* tp_getset */ |
| 2907 | 0, /* tp_base */ |
| 2908 | 0, /* tp_dict */ |
| 2909 | 0, /* tp_descr_get */ |
| 2910 | 0, /* tp_descr_set */ |
| 2911 | 0, /* tp_dictoffset */ |
| 2912 | 0, /* tp_init */ |
| 2913 | 0, /* tp_alloc */ |
| 2914 | date_new, /* tp_new */ |
| 2915 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2916 | }; |
| 2917 | |
| 2918 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2919 | * PyDateTime_TZInfo implementation. |
| 2920 | */ |
| 2921 | |
| 2922 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 2923 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 2924 | * 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] | 2925 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2926 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 2927 | * |
| 2928 | * Note: For reasons having to do with pickling of subclasses, we have |
| 2929 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 2930 | * in the Python implementation (__init__() could raise NotImplementedError |
| 2931 | * there without ill effect), but doing so in the C implementation hit a |
| 2932 | * brick wall. |
| 2933 | */ |
| 2934 | |
| 2935 | static PyObject * |
| 2936 | tzinfo_nogo(const char* methodname) |
| 2937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2938 | PyErr_Format(PyExc_NotImplementedError, |
| 2939 | "a tzinfo subclass must implement %s()", |
| 2940 | methodname); |
| 2941 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | /* Methods. A subclass must implement these. */ |
| 2945 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2946 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2947 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 2948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2949 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2952 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2953 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 2954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2955 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2958 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2959 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 2960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2961 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2964 | static PyObject * |
| 2965 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyDateTime_DateTime *dt) |
| 2966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2967 | int y, m, d, hh, mm, ss, us; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2969 | PyObject *result; |
| 2970 | int off, dst; |
| 2971 | int none; |
| 2972 | int delta; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2974 | if (! PyDateTime_Check(dt)) { |
| 2975 | PyErr_SetString(PyExc_TypeError, |
| 2976 | "fromutc: argument must be a datetime"); |
| 2977 | return NULL; |
| 2978 | } |
| 2979 | if (! HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
| 2980 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 2981 | "is not self"); |
| 2982 | return NULL; |
| 2983 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2985 | off = call_utcoffset(dt->tzinfo, (PyObject *)dt, &none); |
| 2986 | if (off == -1 && PyErr_Occurred()) |
| 2987 | return NULL; |
| 2988 | if (none) { |
| 2989 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 2990 | "utcoffset() result required"); |
| 2991 | return NULL; |
| 2992 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 2994 | dst = call_dst(dt->tzinfo, (PyObject *)dt, &none); |
| 2995 | if (dst == -1 && PyErr_Occurred()) |
| 2996 | return NULL; |
| 2997 | if (none) { |
| 2998 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 2999 | "dst() result required"); |
| 3000 | return NULL; |
| 3001 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3003 | y = GET_YEAR(dt); |
| 3004 | m = GET_MONTH(dt); |
| 3005 | d = GET_DAY(dt); |
| 3006 | hh = DATE_GET_HOUR(dt); |
| 3007 | mm = DATE_GET_MINUTE(dt); |
| 3008 | ss = DATE_GET_SECOND(dt); |
| 3009 | us = DATE_GET_MICROSECOND(dt); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3011 | delta = off - dst; |
| 3012 | mm += delta; |
| 3013 | if ((mm < 0 || mm >= 60) && |
| 3014 | normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
| 3015 | return NULL; |
| 3016 | result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo); |
| 3017 | if (result == NULL) |
| 3018 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3020 | dst = call_dst(dt->tzinfo, result, &none); |
| 3021 | if (dst == -1 && PyErr_Occurred()) |
| 3022 | goto Fail; |
| 3023 | if (none) |
| 3024 | goto Inconsistent; |
| 3025 | if (dst == 0) |
| 3026 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3028 | mm += dst; |
| 3029 | if ((mm < 0 || mm >= 60) && |
| 3030 | normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
| 3031 | goto Fail; |
| 3032 | Py_DECREF(result); |
| 3033 | result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo); |
| 3034 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3035 | |
| 3036 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3037 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3038 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3040 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3041 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3042 | Py_DECREF(result); |
| 3043 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3046 | /* |
| 3047 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3048 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3049 | */ |
| 3050 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3051 | static PyObject * |
| 3052 | tzinfo_reduce(PyObject *self) |
| 3053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3054 | PyObject *args, *state, *tmp; |
| 3055 | PyObject *getinitargs, *getstate; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3057 | tmp = PyTuple_New(0); |
| 3058 | if (tmp == NULL) |
| 3059 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3061 | getinitargs = PyObject_GetAttrString(self, "__getinitargs__"); |
| 3062 | if (getinitargs != NULL) { |
| 3063 | args = PyObject_CallObject(getinitargs, tmp); |
| 3064 | Py_DECREF(getinitargs); |
| 3065 | if (args == NULL) { |
| 3066 | Py_DECREF(tmp); |
| 3067 | return NULL; |
| 3068 | } |
| 3069 | } |
| 3070 | else { |
| 3071 | PyErr_Clear(); |
| 3072 | args = tmp; |
| 3073 | Py_INCREF(args); |
| 3074 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3076 | getstate = PyObject_GetAttrString(self, "__getstate__"); |
| 3077 | if (getstate != NULL) { |
| 3078 | state = PyObject_CallObject(getstate, tmp); |
| 3079 | Py_DECREF(getstate); |
| 3080 | if (state == NULL) { |
| 3081 | Py_DECREF(args); |
| 3082 | Py_DECREF(tmp); |
| 3083 | return NULL; |
| 3084 | } |
| 3085 | } |
| 3086 | else { |
| 3087 | PyObject **dictptr; |
| 3088 | PyErr_Clear(); |
| 3089 | state = Py_None; |
| 3090 | dictptr = _PyObject_GetDictPtr(self); |
| 3091 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
| 3092 | state = *dictptr; |
| 3093 | Py_INCREF(state); |
| 3094 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3096 | Py_DECREF(tmp); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3098 | if (state == Py_None) { |
| 3099 | Py_DECREF(state); |
| 3100 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3101 | } |
| 3102 | else |
| 3103 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3104 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3105 | |
| 3106 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3108 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3109 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3111 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
| 3112 | PyDoc_STR("datetime -> minutes east of UTC (negative for " |
| 3113 | "west of UTC).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3115 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3116 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3118 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
| 3119 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3121 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3122 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3124 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3125 | }; |
| 3126 | |
| 3127 | static char tzinfo_doc[] = |
| 3128 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3129 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3130 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3131 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3132 | "datetime.tzinfo", /* tp_name */ |
| 3133 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3134 | 0, /* tp_itemsize */ |
| 3135 | 0, /* tp_dealloc */ |
| 3136 | 0, /* tp_print */ |
| 3137 | 0, /* tp_getattr */ |
| 3138 | 0, /* tp_setattr */ |
| 3139 | 0, /* tp_reserved */ |
| 3140 | 0, /* tp_repr */ |
| 3141 | 0, /* tp_as_number */ |
| 3142 | 0, /* tp_as_sequence */ |
| 3143 | 0, /* tp_as_mapping */ |
| 3144 | 0, /* tp_hash */ |
| 3145 | 0, /* tp_call */ |
| 3146 | 0, /* tp_str */ |
| 3147 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3148 | 0, /* tp_setattro */ |
| 3149 | 0, /* tp_as_buffer */ |
| 3150 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3151 | tzinfo_doc, /* tp_doc */ |
| 3152 | 0, /* tp_traverse */ |
| 3153 | 0, /* tp_clear */ |
| 3154 | 0, /* tp_richcompare */ |
| 3155 | 0, /* tp_weaklistoffset */ |
| 3156 | 0, /* tp_iter */ |
| 3157 | 0, /* tp_iternext */ |
| 3158 | tzinfo_methods, /* tp_methods */ |
| 3159 | 0, /* tp_members */ |
| 3160 | 0, /* tp_getset */ |
| 3161 | 0, /* tp_base */ |
| 3162 | 0, /* tp_dict */ |
| 3163 | 0, /* tp_descr_get */ |
| 3164 | 0, /* tp_descr_set */ |
| 3165 | 0, /* tp_dictoffset */ |
| 3166 | 0, /* tp_init */ |
| 3167 | 0, /* tp_alloc */ |
| 3168 | PyType_GenericNew, /* tp_new */ |
| 3169 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3170 | }; |
| 3171 | |
| 3172 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3173 | * PyDateTime_Time implementation. |
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 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3177 | */ |
| 3178 | |
| 3179 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3180 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3182 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3185 | static PyObject * |
| 3186 | time_minute(PyDateTime_Time *self, void *unused) |
| 3187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3188 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | /* The name time_second conflicted with some platform header file. */ |
| 3192 | static PyObject * |
| 3193 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3195 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | static PyObject * |
| 3199 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3201 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | static PyObject * |
| 3205 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3207 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3208 | Py_INCREF(result); |
| 3209 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3213 | {"hour", (getter)time_hour}, |
| 3214 | {"minute", (getter)time_minute}, |
| 3215 | {"second", (getter)py_time_second}, |
| 3216 | {"microsecond", (getter)time_microsecond}, |
| 3217 | {"tzinfo", (getter)time_tzinfo}, |
| 3218 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3219 | }; |
| 3220 | |
| 3221 | /* |
| 3222 | * Constructors. |
| 3223 | */ |
| 3224 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3225 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3226 | "tzinfo", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3227 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3228 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3229 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3231 | PyObject *self = NULL; |
| 3232 | PyObject *state; |
| 3233 | int hour = 0; |
| 3234 | int minute = 0; |
| 3235 | int second = 0; |
| 3236 | int usecond = 0; |
| 3237 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3239 | /* Check for invocation from pickle with __getstate__ state */ |
| 3240 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3241 | PyTuple_GET_SIZE(args) <= 2 && |
| 3242 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3243 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 3244 | ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24) |
| 3245 | { |
| 3246 | PyDateTime_Time *me; |
| 3247 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3249 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3250 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3251 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3252 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3253 | "tzinfo state arg"); |
| 3254 | return NULL; |
| 3255 | } |
| 3256 | } |
| 3257 | aware = (char)(tzinfo != Py_None); |
| 3258 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3259 | if (me != NULL) { |
| 3260 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3262 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3263 | me->hashcode = -1; |
| 3264 | me->hastzinfo = aware; |
| 3265 | if (aware) { |
| 3266 | Py_INCREF(tzinfo); |
| 3267 | me->tzinfo = tzinfo; |
| 3268 | } |
| 3269 | } |
| 3270 | return (PyObject *)me; |
| 3271 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3273 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
| 3274 | &hour, &minute, &second, &usecond, |
| 3275 | &tzinfo)) { |
| 3276 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3277 | return NULL; |
| 3278 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3279 | return NULL; |
| 3280 | self = new_time_ex(hour, minute, second, usecond, tzinfo, |
| 3281 | type); |
| 3282 | } |
| 3283 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* |
| 3287 | * Destructor. |
| 3288 | */ |
| 3289 | |
| 3290 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3291 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3293 | if (HASTZINFO(self)) { |
| 3294 | Py_XDECREF(self->tzinfo); |
| 3295 | } |
| 3296 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3300 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3301 | */ |
| 3302 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3303 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3304 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3305 | time_utcoffset(PyDateTime_Time *self, PyObject *unused) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3306 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3307 | "utcoffset", Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3311 | time_dst(PyDateTime_Time *self, PyObject *unused) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3312 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3313 | "dst", Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3317 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3318 | return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3319 | Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3323 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3324 | */ |
| 3325 | |
| 3326 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3327 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3329 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3330 | int h = TIME_GET_HOUR(self); |
| 3331 | int m = TIME_GET_MINUTE(self); |
| 3332 | int s = TIME_GET_SECOND(self); |
| 3333 | int us = TIME_GET_MICROSECOND(self); |
| 3334 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3336 | if (us) |
| 3337 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3338 | type_name, h, m, s, us); |
| 3339 | else if (s) |
| 3340 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3341 | type_name, h, m, s); |
| 3342 | else |
| 3343 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3344 | if (result != NULL && HASTZINFO(self)) |
| 3345 | result = append_keyword_tzinfo(result, self->tzinfo); |
| 3346 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3349 | static PyObject * |
| 3350 | time_str(PyDateTime_Time *self) |
| 3351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3352 | return PyObject_CallMethod((PyObject *)self, "isoformat", "()"); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3353 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3354 | |
| 3355 | static PyObject * |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3356 | time_isoformat(PyDateTime_Time *self, PyObject *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3358 | char buf[100]; |
| 3359 | PyObject *result; |
| 3360 | int us = TIME_GET_MICROSECOND(self);; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3362 | if (us) |
| 3363 | result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d", |
| 3364 | TIME_GET_HOUR(self), |
| 3365 | TIME_GET_MINUTE(self), |
| 3366 | TIME_GET_SECOND(self), |
| 3367 | us); |
| 3368 | else |
| 3369 | result = PyUnicode_FromFormat("%02d:%02d:%02d", |
| 3370 | TIME_GET_HOUR(self), |
| 3371 | TIME_GET_MINUTE(self), |
| 3372 | TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3374 | if (result == NULL || ! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3375 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3377 | /* We need to append the UTC offset. */ |
| 3378 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3379 | Py_None) < 0) { |
| 3380 | Py_DECREF(result); |
| 3381 | return NULL; |
| 3382 | } |
| 3383 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3384 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3387 | static PyObject * |
| 3388 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3390 | PyObject *result; |
| 3391 | PyObject *tuple; |
| 3392 | PyObject *format; |
| 3393 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3395 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3396 | &format)) |
| 3397 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3399 | /* Python's strftime does insane things with the year part of the |
| 3400 | * timetuple. The year is forced to (the otherwise nonsensical) |
| 3401 | * 1900 to worm around that. |
| 3402 | */ |
| 3403 | tuple = Py_BuildValue("iiiiiiiii", |
| 3404 | 1900, 1, 1, /* year, month, day */ |
| 3405 | TIME_GET_HOUR(self), |
| 3406 | TIME_GET_MINUTE(self), |
| 3407 | TIME_GET_SECOND(self), |
| 3408 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3409 | if (tuple == NULL) |
| 3410 | return NULL; |
| 3411 | assert(PyTuple_Size(tuple) == 9); |
| 3412 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3413 | Py_None); |
| 3414 | Py_DECREF(tuple); |
| 3415 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3416 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3417 | |
| 3418 | /* |
| 3419 | * Miscellaneous methods. |
| 3420 | */ |
| 3421 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3422 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3423 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3425 | int diff; |
| 3426 | naivety n1, n2; |
| 3427 | int offset1, offset2; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3429 | if (! PyTime_Check(other)) { |
| 3430 | Py_INCREF(Py_NotImplemented); |
| 3431 | return Py_NotImplemented; |
| 3432 | } |
| 3433 | if (classify_two_utcoffsets(self, &offset1, &n1, Py_None, |
| 3434 | other, &offset2, &n2, Py_None) < 0) |
| 3435 | return NULL; |
| 3436 | assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
| 3437 | /* If they're both naive, or both aware and have the same offsets, |
| 3438 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3439 | * offset2 == 0 at this point. |
| 3440 | */ |
| 3441 | if (n1 == n2 && offset1 == offset2) { |
| 3442 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3443 | ((PyDateTime_Time *)other)->data, |
| 3444 | _PyDateTime_TIME_DATASIZE); |
| 3445 | return diff_to_bool(diff, op); |
| 3446 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3448 | if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) { |
| 3449 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3450 | /* Convert everything except microseconds to seconds. These |
| 3451 | * can't overflow (no more than the # of seconds in 2 days). |
| 3452 | */ |
| 3453 | offset1 = TIME_GET_HOUR(self) * 3600 + |
| 3454 | (TIME_GET_MINUTE(self) - offset1) * 60 + |
| 3455 | TIME_GET_SECOND(self); |
| 3456 | offset2 = TIME_GET_HOUR(other) * 3600 + |
| 3457 | (TIME_GET_MINUTE(other) - offset2) * 60 + |
| 3458 | TIME_GET_SECOND(other); |
| 3459 | diff = offset1 - offset2; |
| 3460 | if (diff == 0) |
| 3461 | diff = TIME_GET_MICROSECOND(self) - |
| 3462 | TIME_GET_MICROSECOND(other); |
| 3463 | return diff_to_bool(diff, op); |
| 3464 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3466 | assert(n1 != n2); |
| 3467 | PyErr_SetString(PyExc_TypeError, |
| 3468 | "can't compare offset-naive and " |
| 3469 | "offset-aware times"); |
| 3470 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | static long |
| 3474 | time_hash(PyDateTime_Time *self) |
| 3475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3476 | if (self->hashcode == -1) { |
| 3477 | naivety n; |
| 3478 | int offset; |
| 3479 | PyObject *temp; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3481 | n = classify_utcoffset((PyObject *)self, Py_None, &offset); |
| 3482 | assert(n != OFFSET_UNKNOWN); |
| 3483 | if (n == OFFSET_ERROR) |
| 3484 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3486 | /* Reduce this to a hash of another object. */ |
| 3487 | if (offset == 0) { |
| 3488 | self->hashcode = generic_hash( |
| 3489 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
| 3490 | return self->hashcode; |
| 3491 | } |
| 3492 | else { |
| 3493 | int hour; |
| 3494 | int minute; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3496 | assert(n == OFFSET_AWARE); |
| 3497 | assert(HASTZINFO(self)); |
| 3498 | hour = divmod(TIME_GET_HOUR(self) * 60 + |
| 3499 | TIME_GET_MINUTE(self) - offset, |
| 3500 | 60, |
| 3501 | &minute); |
| 3502 | if (0 <= hour && hour < 24) |
| 3503 | temp = new_time(hour, minute, |
| 3504 | TIME_GET_SECOND(self), |
| 3505 | TIME_GET_MICROSECOND(self), |
| 3506 | Py_None); |
| 3507 | else |
| 3508 | temp = Py_BuildValue("iiii", |
| 3509 | hour, minute, |
| 3510 | TIME_GET_SECOND(self), |
| 3511 | TIME_GET_MICROSECOND(self)); |
| 3512 | } |
| 3513 | if (temp != NULL) { |
| 3514 | self->hashcode = PyObject_Hash(temp); |
| 3515 | Py_DECREF(temp); |
| 3516 | } |
| 3517 | } |
| 3518 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3519 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3520 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3521 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3522 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3524 | PyObject *clone; |
| 3525 | PyObject *tuple; |
| 3526 | int hh = TIME_GET_HOUR(self); |
| 3527 | int mm = TIME_GET_MINUTE(self); |
| 3528 | int ss = TIME_GET_SECOND(self); |
| 3529 | int us = TIME_GET_MICROSECOND(self); |
| 3530 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3532 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
| 3533 | time_kws, |
| 3534 | &hh, &mm, &ss, &us, &tzinfo)) |
| 3535 | return NULL; |
| 3536 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3537 | if (tuple == NULL) |
| 3538 | return NULL; |
| 3539 | clone = time_new(Py_TYPE(self), tuple, NULL); |
| 3540 | Py_DECREF(tuple); |
| 3541 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3544 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3545 | time_bool(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3547 | int offset; |
| 3548 | int none; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3550 | if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { |
| 3551 | /* Since utcoffset is in whole minutes, nothing can |
| 3552 | * alter the conclusion that this is nonzero. |
| 3553 | */ |
| 3554 | return 1; |
| 3555 | } |
| 3556 | offset = 0; |
| 3557 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 3558 | offset = call_utcoffset(self->tzinfo, Py_None, &none); |
| 3559 | if (offset == -1 && PyErr_Occurred()) |
| 3560 | return -1; |
| 3561 | } |
| 3562 | return (TIME_GET_MINUTE(self) - offset + TIME_GET_HOUR(self)*60) != 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3565 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3566 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3567 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3568 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3569 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3570 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3571 | */ |
| 3572 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3573 | time_getstate(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3575 | PyObject *basestate; |
| 3576 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3578 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3579 | _PyDateTime_TIME_DATASIZE); |
| 3580 | if (basestate != NULL) { |
| 3581 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3582 | result = PyTuple_Pack(1, basestate); |
| 3583 | else |
| 3584 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3585 | Py_DECREF(basestate); |
| 3586 | } |
| 3587 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3591 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3593 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3596 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3598 | {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
| 3599 | PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
| 3600 | "[+HH:MM].")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3602 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3603 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3605 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3606 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3608 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 3609 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3611 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 3612 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3614 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 3615 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3617 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 3618 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3620 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 3621 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3623 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3624 | }; |
| 3625 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3626 | static char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3627 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 3628 | \n\ |
| 3629 | All arguments are optional. tzinfo may be None, or an instance of\n\ |
| 3630 | a tzinfo subclass. The remaining arguments may be ints or longs.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3631 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3632 | static PyNumberMethods time_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3633 | 0, /* nb_add */ |
| 3634 | 0, /* nb_subtract */ |
| 3635 | 0, /* nb_multiply */ |
| 3636 | 0, /* nb_remainder */ |
| 3637 | 0, /* nb_divmod */ |
| 3638 | 0, /* nb_power */ |
| 3639 | 0, /* nb_negative */ |
| 3640 | 0, /* nb_positive */ |
| 3641 | 0, /* nb_absolute */ |
| 3642 | (inquiry)time_bool, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3643 | }; |
| 3644 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3645 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3646 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3647 | "datetime.time", /* tp_name */ |
| 3648 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 3649 | 0, /* tp_itemsize */ |
| 3650 | (destructor)time_dealloc, /* tp_dealloc */ |
| 3651 | 0, /* tp_print */ |
| 3652 | 0, /* tp_getattr */ |
| 3653 | 0, /* tp_setattr */ |
| 3654 | 0, /* tp_reserved */ |
| 3655 | (reprfunc)time_repr, /* tp_repr */ |
| 3656 | &time_as_number, /* tp_as_number */ |
| 3657 | 0, /* tp_as_sequence */ |
| 3658 | 0, /* tp_as_mapping */ |
| 3659 | (hashfunc)time_hash, /* tp_hash */ |
| 3660 | 0, /* tp_call */ |
| 3661 | (reprfunc)time_str, /* tp_str */ |
| 3662 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3663 | 0, /* tp_setattro */ |
| 3664 | 0, /* tp_as_buffer */ |
| 3665 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3666 | time_doc, /* tp_doc */ |
| 3667 | 0, /* tp_traverse */ |
| 3668 | 0, /* tp_clear */ |
| 3669 | time_richcompare, /* tp_richcompare */ |
| 3670 | 0, /* tp_weaklistoffset */ |
| 3671 | 0, /* tp_iter */ |
| 3672 | 0, /* tp_iternext */ |
| 3673 | time_methods, /* tp_methods */ |
| 3674 | 0, /* tp_members */ |
| 3675 | time_getset, /* tp_getset */ |
| 3676 | 0, /* tp_base */ |
| 3677 | 0, /* tp_dict */ |
| 3678 | 0, /* tp_descr_get */ |
| 3679 | 0, /* tp_descr_set */ |
| 3680 | 0, /* tp_dictoffset */ |
| 3681 | 0, /* tp_init */ |
| 3682 | time_alloc, /* tp_alloc */ |
| 3683 | time_new, /* tp_new */ |
| 3684 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3685 | }; |
| 3686 | |
| 3687 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3688 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3689 | */ |
| 3690 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3691 | /* Accessor properties. Properties for day, month, and year are inherited |
| 3692 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3693 | */ |
| 3694 | |
| 3695 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3696 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3698 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3701 | static PyObject * |
| 3702 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 3703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3704 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | static PyObject * |
| 3708 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 3709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3710 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | static PyObject * |
| 3714 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 3715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3716 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | static PyObject * |
| 3720 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 3721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3722 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3723 | Py_INCREF(result); |
| 3724 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3728 | {"hour", (getter)datetime_hour}, |
| 3729 | {"minute", (getter)datetime_minute}, |
| 3730 | {"second", (getter)datetime_second}, |
| 3731 | {"microsecond", (getter)datetime_microsecond}, |
| 3732 | {"tzinfo", (getter)datetime_tzinfo}, |
| 3733 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3734 | }; |
| 3735 | |
| 3736 | /* |
| 3737 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3738 | */ |
| 3739 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3740 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3741 | "year", "month", "day", "hour", "minute", "second", |
| 3742 | "microsecond", "tzinfo", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3743 | }; |
| 3744 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3745 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3746 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3748 | PyObject *self = NULL; |
| 3749 | PyObject *state; |
| 3750 | int year; |
| 3751 | int month; |
| 3752 | int day; |
| 3753 | int hour = 0; |
| 3754 | int minute = 0; |
| 3755 | int second = 0; |
| 3756 | int usecond = 0; |
| 3757 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3759 | /* Check for invocation from pickle with __getstate__ state */ |
| 3760 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3761 | PyTuple_GET_SIZE(args) <= 2 && |
| 3762 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3763 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 3764 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 3765 | { |
| 3766 | PyDateTime_DateTime *me; |
| 3767 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3769 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3770 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3771 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3772 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3773 | "tzinfo state arg"); |
| 3774 | return NULL; |
| 3775 | } |
| 3776 | } |
| 3777 | aware = (char)(tzinfo != Py_None); |
| 3778 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 3779 | if (me != NULL) { |
| 3780 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3782 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 3783 | me->hashcode = -1; |
| 3784 | me->hastzinfo = aware; |
| 3785 | if (aware) { |
| 3786 | Py_INCREF(tzinfo); |
| 3787 | me->tzinfo = tzinfo; |
| 3788 | } |
| 3789 | } |
| 3790 | return (PyObject *)me; |
| 3791 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3793 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
| 3794 | &year, &month, &day, &hour, &minute, |
| 3795 | &second, &usecond, &tzinfo)) { |
| 3796 | if (check_date_args(year, month, day) < 0) |
| 3797 | return NULL; |
| 3798 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3799 | return NULL; |
| 3800 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3801 | return NULL; |
| 3802 | self = new_datetime_ex(year, month, day, |
| 3803 | hour, minute, second, usecond, |
| 3804 | tzinfo, type); |
| 3805 | } |
| 3806 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3809 | /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
| 3810 | typedef struct tm *(*TM_FUNC)(const time_t *timer); |
| 3811 | |
| 3812 | /* Internal helper. |
| 3813 | * Build datetime from a time_t and a distinct count of microseconds. |
| 3814 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 3815 | */ |
| 3816 | static PyObject * |
| 3817 | datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3818 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3820 | struct tm *tm; |
| 3821 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3823 | tm = f(&timet); |
| 3824 | if (tm) { |
| 3825 | /* The platform localtime/gmtime may insert leap seconds, |
| 3826 | * indicated by tm->tm_sec > 59. We don't care about them, |
| 3827 | * except to the extent that passing them on to the datetime |
| 3828 | * constructor would raise ValueError for a reason that |
| 3829 | * made no sense to the user. |
| 3830 | */ |
| 3831 | if (tm->tm_sec > 59) |
| 3832 | tm->tm_sec = 59; |
| 3833 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 3834 | tm->tm_year + 1900, |
| 3835 | tm->tm_mon + 1, |
| 3836 | tm->tm_mday, |
| 3837 | tm->tm_hour, |
| 3838 | tm->tm_min, |
| 3839 | tm->tm_sec, |
| 3840 | us, |
| 3841 | tzinfo); |
| 3842 | } |
| 3843 | else |
| 3844 | PyErr_SetString(PyExc_ValueError, |
| 3845 | "timestamp out of range for " |
| 3846 | "platform localtime()/gmtime() function"); |
| 3847 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | /* Internal helper. |
| 3851 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 3852 | * to control the interpretation of the timestamp. Since a double doesn't |
| 3853 | * have enough bits to cover a datetime's full range of precision, it's |
| 3854 | * better to call datetime_from_timet_and_us provided you have a way |
| 3855 | * to get that much precision (e.g., C time() isn't good enough). |
| 3856 | */ |
| 3857 | static PyObject * |
| 3858 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3859 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3861 | time_t timet; |
| 3862 | double fraction; |
| 3863 | int us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3865 | timet = _PyTime_DoubleToTimet(timestamp); |
| 3866 | if (timet == (time_t)-1 && PyErr_Occurred()) |
| 3867 | return NULL; |
| 3868 | fraction = timestamp - (double)timet; |
| 3869 | us = (int)round_to_long(fraction * 1e6); |
| 3870 | if (us < 0) { |
| 3871 | /* Truncation towards zero is not what we wanted |
| 3872 | for negative numbers (Python's mod semantics) */ |
| 3873 | timet -= 1; |
| 3874 | us += 1000000; |
| 3875 | } |
| 3876 | /* If timestamp is less than one microsecond smaller than a |
| 3877 | * full second, round up. Otherwise, ValueErrors are raised |
| 3878 | * for some floats. */ |
| 3879 | if (us == 1000000) { |
| 3880 | timet += 1; |
| 3881 | us = 0; |
| 3882 | } |
| 3883 | return datetime_from_timet_and_us(cls, f, timet, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | /* Internal helper. |
| 3887 | * Build most accurate possible datetime for current time. Pass localtime or |
| 3888 | * gmtime for f as appropriate. |
| 3889 | */ |
| 3890 | static PyObject * |
| 3891 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 3892 | { |
| 3893 | #ifdef HAVE_GETTIMEOFDAY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3894 | struct timeval t; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3895 | |
| 3896 | #ifdef GETTIMEOFDAY_NO_TZ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3897 | gettimeofday(&t); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3898 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3899 | gettimeofday(&t, (struct timezone *)NULL); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3900 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3901 | return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, |
| 3902 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3904 | #else /* ! HAVE_GETTIMEOFDAY */ |
| 3905 | /* No flavor of gettimeofday exists on this platform. Python's |
| 3906 | * time.time() does a lot of other platform tricks to get the |
| 3907 | * best time it can on the platform, and we're not going to do |
| 3908 | * better than that (if we could, the better code would belong |
| 3909 | * in time.time()!) We're limited by the precision of a double, |
| 3910 | * though. |
| 3911 | */ |
| 3912 | PyObject *time; |
| 3913 | double dtime; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3915 | time = time_time(); |
| 3916 | if (time == NULL) |
| 3917 | return NULL; |
| 3918 | dtime = PyFloat_AsDouble(time); |
| 3919 | Py_DECREF(time); |
| 3920 | if (dtime == -1.0 && PyErr_Occurred()) |
| 3921 | return NULL; |
| 3922 | return datetime_from_timestamp(cls, f, dtime, tzinfo); |
| 3923 | #endif /* ! HAVE_GETTIMEOFDAY */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3926 | /* Return best possible local time -- this isn't constrained by the |
| 3927 | * precision of a timestamp. |
| 3928 | */ |
| 3929 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3930 | datetime_now(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3932 | PyObject *self; |
| 3933 | PyObject *tzinfo = Py_None; |
| 3934 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3936 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords, |
| 3937 | &tzinfo)) |
| 3938 | return NULL; |
| 3939 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3940 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 3941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3942 | self = datetime_best_possible(cls, |
| 3943 | tzinfo == Py_None ? localtime : gmtime, |
| 3944 | tzinfo); |
| 3945 | if (self != NULL && tzinfo != Py_None) { |
| 3946 | /* Convert UTC to tzinfo's zone. */ |
| 3947 | PyObject *temp = self; |
| 3948 | self = PyObject_CallMethod(tzinfo, "fromutc", "O", self); |
| 3949 | Py_DECREF(temp); |
| 3950 | } |
| 3951 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3954 | /* Return best possible UTC time -- this isn't constrained by the |
| 3955 | * precision of a timestamp. |
| 3956 | */ |
| 3957 | static PyObject * |
| 3958 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 3959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3960 | return datetime_best_possible(cls, gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3963 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 3964 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3965 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3967 | PyObject *self; |
| 3968 | double timestamp; |
| 3969 | PyObject *tzinfo = Py_None; |
| 3970 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3972 | if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp", |
| 3973 | keywords, ×tamp, &tzinfo)) |
| 3974 | return NULL; |
| 3975 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3976 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 3977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3978 | self = datetime_from_timestamp(cls, |
| 3979 | tzinfo == Py_None ? localtime : gmtime, |
| 3980 | timestamp, |
| 3981 | tzinfo); |
| 3982 | if (self != NULL && tzinfo != Py_None) { |
| 3983 | /* Convert UTC to tzinfo's zone. */ |
| 3984 | PyObject *temp = self; |
| 3985 | self = PyObject_CallMethod(tzinfo, "fromutc", "O", self); |
| 3986 | Py_DECREF(temp); |
| 3987 | } |
| 3988 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3991 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 3992 | static PyObject * |
| 3993 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 3994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3995 | double timestamp; |
| 3996 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 3998 | if (PyArg_ParseTuple(args, "d:utcfromtimestamp", ×tamp)) |
| 3999 | result = datetime_from_timestamp(cls, gmtime, timestamp, |
| 4000 | Py_None); |
| 4001 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4004 | /* Return new datetime from time.strptime(). */ |
| 4005 | static PyObject * |
| 4006 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4008 | static PyObject *module = NULL; |
| 4009 | PyObject *result = NULL, *obj, *st = NULL, *frac = NULL; |
| 4010 | const Py_UNICODE *string, *format; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4012 | if (!PyArg_ParseTuple(args, "uu:strptime", &string, &format)) |
| 4013 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4015 | if (module == NULL && |
| 4016 | (module = PyImport_ImportModuleNoBlock("_strptime")) == NULL) |
| 4017 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4019 | /* _strptime._strptime returns a two-element tuple. The first |
| 4020 | element is a time.struct_time object. The second is the |
| 4021 | microseconds (which are not defined for time.struct_time). */ |
| 4022 | obj = PyObject_CallMethod(module, "_strptime", "uu", string, format); |
| 4023 | if (obj != NULL) { |
| 4024 | int i, good_timetuple = 1; |
| 4025 | long int ia[7]; |
| 4026 | if (PySequence_Check(obj) && PySequence_Size(obj) == 2) { |
| 4027 | st = PySequence_GetItem(obj, 0); |
| 4028 | frac = PySequence_GetItem(obj, 1); |
| 4029 | if (st == NULL || frac == NULL) |
| 4030 | good_timetuple = 0; |
| 4031 | /* copy y/m/d/h/m/s values out of the |
| 4032 | time.struct_time */ |
| 4033 | if (good_timetuple && |
| 4034 | PySequence_Check(st) && |
| 4035 | PySequence_Size(st) >= 6) { |
| 4036 | for (i=0; i < 6; i++) { |
| 4037 | PyObject *p = PySequence_GetItem(st, i); |
| 4038 | if (p == NULL) { |
| 4039 | good_timetuple = 0; |
| 4040 | break; |
| 4041 | } |
| 4042 | if (PyLong_Check(p)) |
| 4043 | ia[i] = PyLong_AsLong(p); |
| 4044 | else |
| 4045 | good_timetuple = 0; |
| 4046 | Py_DECREF(p); |
| 4047 | } |
| 4048 | /* if (PyLong_CheckExact(p)) { |
| 4049 | ia[i] = PyLong_AsLongAndOverflow(p, &overflow); |
| 4050 | if (overflow) |
| 4051 | good_timetuple = 0; |
| 4052 | } |
| 4053 | else |
| 4054 | good_timetuple = 0; |
| 4055 | Py_DECREF(p); |
| 4056 | */ } |
| 4057 | else |
| 4058 | good_timetuple = 0; |
| 4059 | /* follow that up with a little dose of microseconds */ |
| 4060 | if (PyLong_Check(frac)) |
| 4061 | ia[6] = PyLong_AsLong(frac); |
| 4062 | else |
| 4063 | good_timetuple = 0; |
| 4064 | } |
| 4065 | else |
| 4066 | good_timetuple = 0; |
| 4067 | if (good_timetuple) |
| 4068 | result = PyObject_CallFunction(cls, "iiiiiii", |
| 4069 | ia[0], ia[1], ia[2], |
| 4070 | ia[3], ia[4], ia[5], |
| 4071 | ia[6]); |
| 4072 | else |
| 4073 | PyErr_SetString(PyExc_ValueError, |
| 4074 | "unexpected value from _strptime._strptime"); |
| 4075 | } |
| 4076 | Py_XDECREF(obj); |
| 4077 | Py_XDECREF(st); |
| 4078 | Py_XDECREF(frac); |
| 4079 | return result; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4082 | /* Return new datetime from date/datetime and time arguments. */ |
| 4083 | static PyObject * |
| 4084 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4086 | static char *keywords[] = {"date", "time", NULL}; |
| 4087 | PyObject *date; |
| 4088 | PyObject *time; |
| 4089 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4091 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
| 4092 | &PyDateTime_DateType, &date, |
| 4093 | &PyDateTime_TimeType, &time)) { |
| 4094 | PyObject *tzinfo = Py_None; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4096 | if (HASTZINFO(time)) |
| 4097 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4098 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 4099 | GET_YEAR(date), |
| 4100 | GET_MONTH(date), |
| 4101 | GET_DAY(date), |
| 4102 | TIME_GET_HOUR(time), |
| 4103 | TIME_GET_MINUTE(time), |
| 4104 | TIME_GET_SECOND(time), |
| 4105 | TIME_GET_MICROSECOND(time), |
| 4106 | tzinfo); |
| 4107 | } |
| 4108 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4109 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4110 | |
| 4111 | /* |
| 4112 | * Destructor. |
| 4113 | */ |
| 4114 | |
| 4115 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4116 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4118 | if (HASTZINFO(self)) { |
| 4119 | Py_XDECREF(self->tzinfo); |
| 4120 | } |
| 4121 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | /* |
| 4125 | * Indirect access to tzinfo methods. |
| 4126 | */ |
| 4127 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4128 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4129 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4130 | datetime_utcoffset(PyDateTime_DateTime *self, PyObject *unused) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4131 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4132 | "utcoffset", (PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4136 | datetime_dst(PyDateTime_DateTime *self, PyObject *unused) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4137 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4138 | "dst", (PyObject *)self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4142 | datetime_tzname(PyDateTime_DateTime *self, PyObject *unused) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4143 | return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4144 | (PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4148 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4149 | */ |
| 4150 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4151 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4152 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4153 | */ |
| 4154 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4155 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4156 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4158 | /* Note that the C-level additions can't overflow, because of |
| 4159 | * invariant bounds on the member values. |
| 4160 | */ |
| 4161 | int year = GET_YEAR(date); |
| 4162 | int month = GET_MONTH(date); |
| 4163 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4164 | int hour = DATE_GET_HOUR(date); |
| 4165 | int minute = DATE_GET_MINUTE(date); |
| 4166 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4167 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4168 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4170 | assert(factor == 1 || factor == -1); |
| 4171 | if (normalize_datetime(&year, &month, &day, |
| 4172 | &hour, &minute, &second, µsecond) < 0) |
| 4173 | return NULL; |
| 4174 | else |
| 4175 | return new_datetime(year, month, day, |
| 4176 | hour, minute, second, microsecond, |
| 4177 | HASTZINFO(date) ? date->tzinfo : Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4181 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4183 | if (PyDateTime_Check(left)) { |
| 4184 | /* datetime + ??? */ |
| 4185 | if (PyDelta_Check(right)) |
| 4186 | /* datetime + delta */ |
| 4187 | return add_datetime_timedelta( |
| 4188 | (PyDateTime_DateTime *)left, |
| 4189 | (PyDateTime_Delta *)right, |
| 4190 | 1); |
| 4191 | } |
| 4192 | else if (PyDelta_Check(left)) { |
| 4193 | /* delta + datetime */ |
| 4194 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4195 | (PyDateTime_Delta *) left, |
| 4196 | 1); |
| 4197 | } |
| 4198 | Py_INCREF(Py_NotImplemented); |
| 4199 | return Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4203 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4205 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4207 | if (PyDateTime_Check(left)) { |
| 4208 | /* datetime - ??? */ |
| 4209 | if (PyDateTime_Check(right)) { |
| 4210 | /* datetime - datetime */ |
| 4211 | naivety n1, n2; |
| 4212 | int offset1, offset2; |
| 4213 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4215 | if (classify_two_utcoffsets(left, &offset1, &n1, left, |
| 4216 | right, &offset2, &n2, |
| 4217 | right) < 0) |
| 4218 | return NULL; |
| 4219 | assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
| 4220 | if (n1 != n2) { |
| 4221 | PyErr_SetString(PyExc_TypeError, |
| 4222 | "can't subtract offset-naive and " |
| 4223 | "offset-aware datetimes"); |
| 4224 | return NULL; |
| 4225 | } |
| 4226 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4227 | GET_MONTH(left), |
| 4228 | GET_DAY(left)) - |
| 4229 | ymd_to_ord(GET_YEAR(right), |
| 4230 | GET_MONTH(right), |
| 4231 | GET_DAY(right)); |
| 4232 | /* These can't overflow, since the values are |
| 4233 | * normalized. At most this gives the number of |
| 4234 | * seconds in one day. |
| 4235 | */ |
| 4236 | delta_s = (DATE_GET_HOUR(left) - |
| 4237 | DATE_GET_HOUR(right)) * 3600 + |
| 4238 | (DATE_GET_MINUTE(left) - |
| 4239 | DATE_GET_MINUTE(right)) * 60 + |
| 4240 | (DATE_GET_SECOND(left) - |
| 4241 | DATE_GET_SECOND(right)); |
| 4242 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4243 | DATE_GET_MICROSECOND(right); |
| 4244 | /* (left - offset1) - (right - offset2) = |
| 4245 | * (left - right) + (offset2 - offset1) |
| 4246 | */ |
| 4247 | delta_s += (offset2 - offset1) * 60; |
| 4248 | result = new_delta(delta_d, delta_s, delta_us, 1); |
| 4249 | } |
| 4250 | else if (PyDelta_Check(right)) { |
| 4251 | /* datetime - delta */ |
| 4252 | result = add_datetime_timedelta( |
| 4253 | (PyDateTime_DateTime *)left, |
| 4254 | (PyDateTime_Delta *)right, |
| 4255 | -1); |
| 4256 | } |
| 4257 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4259 | if (result == Py_NotImplemented) |
| 4260 | Py_INCREF(result); |
| 4261 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | /* Various ways to turn a datetime into a string. */ |
| 4265 | |
| 4266 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4267 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4269 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4270 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4272 | if (DATE_GET_MICROSECOND(self)) { |
| 4273 | baserepr = PyUnicode_FromFormat( |
| 4274 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4275 | type_name, |
| 4276 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4277 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4278 | DATE_GET_SECOND(self), |
| 4279 | DATE_GET_MICROSECOND(self)); |
| 4280 | } |
| 4281 | else if (DATE_GET_SECOND(self)) { |
| 4282 | baserepr = PyUnicode_FromFormat( |
| 4283 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4284 | type_name, |
| 4285 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4286 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4287 | DATE_GET_SECOND(self)); |
| 4288 | } |
| 4289 | else { |
| 4290 | baserepr = PyUnicode_FromFormat( |
| 4291 | "%s(%d, %d, %d, %d, %d)", |
| 4292 | type_name, |
| 4293 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4294 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4295 | } |
| 4296 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4297 | return baserepr; |
| 4298 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4301 | static PyObject * |
| 4302 | datetime_str(PyDateTime_DateTime *self) |
| 4303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4304 | return PyObject_CallMethod((PyObject *)self, "isoformat", "(s)", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4305 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4306 | |
| 4307 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4308 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4310 | int sep = 'T'; |
| 4311 | static char *keywords[] = {"sep", NULL}; |
| 4312 | char buffer[100]; |
| 4313 | PyObject *result; |
| 4314 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4316 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep)) |
| 4317 | return NULL; |
| 4318 | if (us) |
| 4319 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d", |
| 4320 | GET_YEAR(self), GET_MONTH(self), |
| 4321 | GET_DAY(self), (int)sep, |
| 4322 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4323 | DATE_GET_SECOND(self), us); |
| 4324 | else |
| 4325 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d", |
| 4326 | GET_YEAR(self), GET_MONTH(self), |
| 4327 | GET_DAY(self), (int)sep, |
| 4328 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4329 | DATE_GET_SECOND(self)); |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4331 | if (!result || !HASTZINFO(self)) |
| 4332 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4334 | /* We need to append the UTC offset. */ |
| 4335 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4336 | (PyObject *)self) < 0) { |
| 4337 | Py_DECREF(result); |
| 4338 | return NULL; |
| 4339 | } |
| 4340 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4341 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4344 | static PyObject * |
| 4345 | datetime_ctime(PyDateTime_DateTime *self) |
| 4346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4347 | return format_ctime((PyDateTime_Date *)self, |
| 4348 | DATE_GET_HOUR(self), |
| 4349 | DATE_GET_MINUTE(self), |
| 4350 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4353 | /* Miscellaneous methods. */ |
| 4354 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4355 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4356 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4358 | int diff; |
| 4359 | naivety n1, n2; |
| 4360 | int offset1, offset2; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4362 | if (! PyDateTime_Check(other)) { |
| 4363 | if (PyDate_Check(other)) { |
| 4364 | /* Prevent invocation of date_richcompare. We want to |
| 4365 | return NotImplemented here to give the other object |
| 4366 | a chance. But since DateTime is a subclass of |
| 4367 | Date, if the other object is a Date, it would |
| 4368 | compute an ordering based on the date part alone, |
| 4369 | and we don't want that. So force unequal or |
| 4370 | uncomparable here in that case. */ |
| 4371 | if (op == Py_EQ) |
| 4372 | Py_RETURN_FALSE; |
| 4373 | if (op == Py_NE) |
| 4374 | Py_RETURN_TRUE; |
| 4375 | return cmperror(self, other); |
| 4376 | } |
| 4377 | Py_INCREF(Py_NotImplemented); |
| 4378 | return Py_NotImplemented; |
| 4379 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4381 | if (classify_two_utcoffsets(self, &offset1, &n1, self, |
| 4382 | other, &offset2, &n2, other) < 0) |
| 4383 | return NULL; |
| 4384 | assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
| 4385 | /* If they're both naive, or both aware and have the same offsets, |
| 4386 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4387 | * offset2 == 0 at this point. |
| 4388 | */ |
| 4389 | if (n1 == n2 && offset1 == offset2) { |
| 4390 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4391 | ((PyDateTime_DateTime *)other)->data, |
| 4392 | _PyDateTime_DATETIME_DATASIZE); |
| 4393 | return diff_to_bool(diff, op); |
| 4394 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4396 | if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) { |
| 4397 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4399 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 4400 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4401 | other); |
| 4402 | if (delta == NULL) |
| 4403 | return NULL; |
| 4404 | diff = GET_TD_DAYS(delta); |
| 4405 | if (diff == 0) |
| 4406 | diff = GET_TD_SECONDS(delta) | |
| 4407 | GET_TD_MICROSECONDS(delta); |
| 4408 | Py_DECREF(delta); |
| 4409 | return diff_to_bool(diff, op); |
| 4410 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4412 | assert(n1 != n2); |
| 4413 | PyErr_SetString(PyExc_TypeError, |
| 4414 | "can't compare offset-naive and " |
| 4415 | "offset-aware datetimes"); |
| 4416 | return NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | static long |
| 4420 | datetime_hash(PyDateTime_DateTime *self) |
| 4421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4422 | if (self->hashcode == -1) { |
| 4423 | naivety n; |
| 4424 | int offset; |
| 4425 | PyObject *temp; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4427 | n = classify_utcoffset((PyObject *)self, (PyObject *)self, |
| 4428 | &offset); |
| 4429 | assert(n != OFFSET_UNKNOWN); |
| 4430 | if (n == OFFSET_ERROR) |
| 4431 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4433 | /* Reduce this to a hash of another object. */ |
| 4434 | if (n == OFFSET_NAIVE) { |
| 4435 | self->hashcode = generic_hash( |
| 4436 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
| 4437 | return self->hashcode; |
| 4438 | } |
| 4439 | else { |
| 4440 | int days; |
| 4441 | int seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4443 | assert(n == OFFSET_AWARE); |
| 4444 | assert(HASTZINFO(self)); |
| 4445 | days = ymd_to_ord(GET_YEAR(self), |
| 4446 | GET_MONTH(self), |
| 4447 | GET_DAY(self)); |
| 4448 | seconds = DATE_GET_HOUR(self) * 3600 + |
| 4449 | (DATE_GET_MINUTE(self) - offset) * 60 + |
| 4450 | DATE_GET_SECOND(self); |
| 4451 | temp = new_delta(days, |
| 4452 | seconds, |
| 4453 | DATE_GET_MICROSECOND(self), |
| 4454 | 1); |
| 4455 | } |
| 4456 | if (temp != NULL) { |
| 4457 | self->hashcode = PyObject_Hash(temp); |
| 4458 | Py_DECREF(temp); |
| 4459 | } |
| 4460 | } |
| 4461 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4462 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4463 | |
| 4464 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4465 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4467 | PyObject *clone; |
| 4468 | PyObject *tuple; |
| 4469 | int y = GET_YEAR(self); |
| 4470 | int m = GET_MONTH(self); |
| 4471 | int d = GET_DAY(self); |
| 4472 | int hh = DATE_GET_HOUR(self); |
| 4473 | int mm = DATE_GET_MINUTE(self); |
| 4474 | int ss = DATE_GET_SECOND(self); |
| 4475 | int us = DATE_GET_MICROSECOND(self); |
| 4476 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4478 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
| 4479 | datetime_kws, |
| 4480 | &y, &m, &d, &hh, &mm, &ss, &us, |
| 4481 | &tzinfo)) |
| 4482 | return NULL; |
| 4483 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 4484 | if (tuple == NULL) |
| 4485 | return NULL; |
| 4486 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
| 4487 | Py_DECREF(tuple); |
| 4488 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4492 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4494 | int y, m, d, hh, mm, ss, us; |
| 4495 | PyObject *result; |
| 4496 | int offset, none; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4498 | PyObject *tzinfo; |
| 4499 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4501 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:astimezone", keywords, |
| 4502 | &PyDateTime_TZInfoType, &tzinfo)) |
| 4503 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4505 | if (!HASTZINFO(self) || self->tzinfo == Py_None) |
| 4506 | goto NeedAware; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4508 | /* Conversion to self's own time zone is a NOP. */ |
| 4509 | if (self->tzinfo == tzinfo) { |
| 4510 | Py_INCREF(self); |
| 4511 | return (PyObject *)self; |
| 4512 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4514 | /* Convert self to UTC. */ |
| 4515 | offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); |
| 4516 | if (offset == -1 && PyErr_Occurred()) |
| 4517 | return NULL; |
| 4518 | if (none) |
| 4519 | goto NeedAware; |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4521 | y = GET_YEAR(self); |
| 4522 | m = GET_MONTH(self); |
| 4523 | d = GET_DAY(self); |
| 4524 | hh = DATE_GET_HOUR(self); |
| 4525 | mm = DATE_GET_MINUTE(self); |
| 4526 | ss = DATE_GET_SECOND(self); |
| 4527 | us = DATE_GET_MICROSECOND(self); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4529 | mm -= offset; |
| 4530 | if ((mm < 0 || mm >= 60) && |
| 4531 | normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
| 4532 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4534 | /* Attach new tzinfo and let fromutc() do the rest. */ |
| 4535 | result = new_datetime(y, m, d, hh, mm, ss, us, tzinfo); |
| 4536 | if (result != NULL) { |
| 4537 | PyObject *temp = result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4539 | result = PyObject_CallMethod(tzinfo, "fromutc", "O", temp); |
| 4540 | Py_DECREF(temp); |
| 4541 | } |
| 4542 | return result; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4543 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4544 | NeedAware: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4545 | PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
| 4546 | "a naive datetime"); |
| 4547 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4551 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4553 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4555 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 4556 | int none; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4558 | dstflag = call_dst(self->tzinfo, (PyObject *)self, &none); |
| 4559 | if (dstflag == -1 && PyErr_Occurred()) |
| 4560 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4562 | if (none) |
| 4563 | dstflag = -1; |
| 4564 | else if (dstflag != 0) |
| 4565 | dstflag = 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4567 | } |
| 4568 | return build_struct_time(GET_YEAR(self), |
| 4569 | GET_MONTH(self), |
| 4570 | GET_DAY(self), |
| 4571 | DATE_GET_HOUR(self), |
| 4572 | DATE_GET_MINUTE(self), |
| 4573 | DATE_GET_SECOND(self), |
| 4574 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4578 | datetime_getdate(PyDateTime_DateTime *self) |
| 4579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4580 | return new_date(GET_YEAR(self), |
| 4581 | GET_MONTH(self), |
| 4582 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | static PyObject * |
| 4586 | datetime_gettime(PyDateTime_DateTime *self) |
| 4587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4588 | return new_time(DATE_GET_HOUR(self), |
| 4589 | DATE_GET_MINUTE(self), |
| 4590 | DATE_GET_SECOND(self), |
| 4591 | DATE_GET_MICROSECOND(self), |
| 4592 | Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | static PyObject * |
| 4596 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 4597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4598 | return new_time(DATE_GET_HOUR(self), |
| 4599 | DATE_GET_MINUTE(self), |
| 4600 | DATE_GET_SECOND(self), |
| 4601 | DATE_GET_MICROSECOND(self), |
| 4602 | HASTZINFO(self) ? self->tzinfo : Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | static PyObject * |
| 4606 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4608 | int y = GET_YEAR(self); |
| 4609 | int m = GET_MONTH(self); |
| 4610 | int d = GET_DAY(self); |
| 4611 | int hh = DATE_GET_HOUR(self); |
| 4612 | int mm = DATE_GET_MINUTE(self); |
| 4613 | int ss = DATE_GET_SECOND(self); |
| 4614 | int us = 0; /* microseconds are ignored in a timetuple */ |
| 4615 | int offset = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4617 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 4618 | int none; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4620 | offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); |
| 4621 | if (offset == -1 && PyErr_Occurred()) |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | /* Even if offset is 0, don't call timetuple() -- tm_isdst should be |
| 4625 | * 0 in a UTC timetuple regardless of what dst() says. |
| 4626 | */ |
| 4627 | if (offset) { |
| 4628 | /* Subtract offset minutes & normalize. */ |
| 4629 | int stat; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4631 | mm -= offset; |
| 4632 | stat = normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us); |
| 4633 | if (stat < 0) { |
| 4634 | /* At the edges, it's possible we overflowed |
| 4635 | * beyond MINYEAR or MAXYEAR. |
| 4636 | */ |
| 4637 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 4638 | PyErr_Clear(); |
| 4639 | else |
| 4640 | return NULL; |
| 4641 | } |
| 4642 | } |
| 4643 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4646 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4647 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4648 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4649 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4650 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4651 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4652 | */ |
| 4653 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4654 | datetime_getstate(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4656 | PyObject *basestate; |
| 4657 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4659 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4660 | _PyDateTime_DATETIME_DATASIZE); |
| 4661 | if (basestate != NULL) { |
| 4662 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4663 | result = PyTuple_Pack(1, basestate); |
| 4664 | else |
| 4665 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4666 | Py_DECREF(basestate); |
| 4667 | } |
| 4668 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4672 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4674 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4677 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4679 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4681 | {"now", (PyCFunction)datetime_now, |
| 4682 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 4683 | PyDoc_STR("[tz] -> new datetime with tz's local day and time.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4685 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 4686 | METH_NOARGS | METH_CLASS, |
| 4687 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4689 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 4690 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 4691 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4693 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 4694 | METH_VARARGS | METH_CLASS, |
| 4695 | PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " |
| 4696 | "(like time.time()).")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4698 | {"strptime", (PyCFunction)datetime_strptime, |
| 4699 | METH_VARARGS | METH_CLASS, |
| 4700 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 4701 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4703 | {"combine", (PyCFunction)datetime_combine, |
| 4704 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 4705 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4707 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4709 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 4710 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4712 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 4713 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4715 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 4716 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4718 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 4719 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4721 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 4722 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4724 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 4725 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4727 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 4728 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
| 4729 | "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
| 4730 | "sep is used to separate the year from the time, and " |
| 4731 | "defaults to 'T'.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4733 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 4734 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4736 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 4737 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4739 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 4740 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4742 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 4743 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4745 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 4746 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4748 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 4749 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4751 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4752 | }; |
| 4753 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4754 | static char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4755 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 4756 | \n\ |
| 4757 | The year, month and day arguments are required. tzinfo may be None, or an\n\ |
| 4758 | 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] | 4759 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4760 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4761 | datetime_add, /* nb_add */ |
| 4762 | datetime_subtract, /* nb_subtract */ |
| 4763 | 0, /* nb_multiply */ |
| 4764 | 0, /* nb_remainder */ |
| 4765 | 0, /* nb_divmod */ |
| 4766 | 0, /* nb_power */ |
| 4767 | 0, /* nb_negative */ |
| 4768 | 0, /* nb_positive */ |
| 4769 | 0, /* nb_absolute */ |
| 4770 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4771 | }; |
| 4772 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4773 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4774 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4775 | "datetime.datetime", /* tp_name */ |
| 4776 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 4777 | 0, /* tp_itemsize */ |
| 4778 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 4779 | 0, /* tp_print */ |
| 4780 | 0, /* tp_getattr */ |
| 4781 | 0, /* tp_setattr */ |
| 4782 | 0, /* tp_reserved */ |
| 4783 | (reprfunc)datetime_repr, /* tp_repr */ |
| 4784 | &datetime_as_number, /* tp_as_number */ |
| 4785 | 0, /* tp_as_sequence */ |
| 4786 | 0, /* tp_as_mapping */ |
| 4787 | (hashfunc)datetime_hash, /* tp_hash */ |
| 4788 | 0, /* tp_call */ |
| 4789 | (reprfunc)datetime_str, /* tp_str */ |
| 4790 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4791 | 0, /* tp_setattro */ |
| 4792 | 0, /* tp_as_buffer */ |
| 4793 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4794 | datetime_doc, /* tp_doc */ |
| 4795 | 0, /* tp_traverse */ |
| 4796 | 0, /* tp_clear */ |
| 4797 | datetime_richcompare, /* tp_richcompare */ |
| 4798 | 0, /* tp_weaklistoffset */ |
| 4799 | 0, /* tp_iter */ |
| 4800 | 0, /* tp_iternext */ |
| 4801 | datetime_methods, /* tp_methods */ |
| 4802 | 0, /* tp_members */ |
| 4803 | datetime_getset, /* tp_getset */ |
| 4804 | &PyDateTime_DateType, /* tp_base */ |
| 4805 | 0, /* tp_dict */ |
| 4806 | 0, /* tp_descr_get */ |
| 4807 | 0, /* tp_descr_set */ |
| 4808 | 0, /* tp_dictoffset */ |
| 4809 | 0, /* tp_init */ |
| 4810 | datetime_alloc, /* tp_alloc */ |
| 4811 | datetime_new, /* tp_new */ |
| 4812 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4813 | }; |
| 4814 | |
| 4815 | /* --------------------------------------------------------------------------- |
| 4816 | * Module methods and initialization. |
| 4817 | */ |
| 4818 | |
| 4819 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4820 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4821 | }; |
| 4822 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 4823 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 4824 | * datetime.h. |
| 4825 | */ |
| 4826 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4827 | &PyDateTime_DateType, |
| 4828 | &PyDateTime_DateTimeType, |
| 4829 | &PyDateTime_TimeType, |
| 4830 | &PyDateTime_DeltaType, |
| 4831 | &PyDateTime_TZInfoType, |
| 4832 | new_date_ex, |
| 4833 | new_datetime_ex, |
| 4834 | new_time_ex, |
| 4835 | new_delta_ex, |
| 4836 | datetime_fromtimestamp, |
| 4837 | date_fromtimestamp |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 4838 | }; |
| 4839 | |
| 4840 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4841 | |
| 4842 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4843 | PyModuleDef_HEAD_INIT, |
| 4844 | "datetime", |
| 4845 | "Fast implementation of the datetime type.", |
| 4846 | -1, |
| 4847 | module_methods, |
| 4848 | NULL, |
| 4849 | NULL, |
| 4850 | NULL, |
| 4851 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4852 | }; |
| 4853 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4854 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4855 | PyInit_datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4857 | PyObject *m; /* a module object */ |
| 4858 | PyObject *d; /* its dict */ |
| 4859 | PyObject *x; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4861 | m = PyModule_Create(&datetimemodule); |
| 4862 | if (m == NULL) |
| 4863 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4865 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 4866 | return NULL; |
| 4867 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 4868 | return NULL; |
| 4869 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 4870 | return NULL; |
| 4871 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 4872 | return NULL; |
| 4873 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 4874 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4876 | /* timedelta values */ |
| 4877 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4879 | x = new_delta(0, 0, 1, 0); |
| 4880 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4881 | return NULL; |
| 4882 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4884 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 4885 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4886 | return NULL; |
| 4887 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4889 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 4890 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4891 | return NULL; |
| 4892 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4894 | /* date values */ |
| 4895 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4897 | x = new_date(1, 1, 1); |
| 4898 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4899 | return NULL; |
| 4900 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4902 | x = new_date(MAXYEAR, 12, 31); |
| 4903 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4904 | return NULL; |
| 4905 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4907 | x = new_delta(1, 0, 0, 0); |
| 4908 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4909 | return NULL; |
| 4910 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4912 | /* time values */ |
| 4913 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4915 | x = new_time(0, 0, 0, 0, Py_None); |
| 4916 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4917 | return NULL; |
| 4918 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4920 | x = new_time(23, 59, 59, 999999, Py_None); |
| 4921 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4922 | return NULL; |
| 4923 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4925 | x = new_delta(0, 0, 1, 0); |
| 4926 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4927 | return NULL; |
| 4928 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4930 | /* datetime values */ |
| 4931 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4933 | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
| 4934 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4935 | return NULL; |
| 4936 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4938 | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
| 4939 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4940 | return NULL; |
| 4941 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4943 | x = new_delta(0, 0, 1, 0); |
| 4944 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4945 | return NULL; |
| 4946 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4948 | /* module initialization */ |
| 4949 | PyModule_AddIntConstant(m, "MINYEAR", MINYEAR); |
| 4950 | PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4952 | Py_INCREF(&PyDateTime_DateType); |
| 4953 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4955 | Py_INCREF(&PyDateTime_DateTimeType); |
| 4956 | PyModule_AddObject(m, "datetime", |
| 4957 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4959 | Py_INCREF(&PyDateTime_TimeType); |
| 4960 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4962 | Py_INCREF(&PyDateTime_DeltaType); |
| 4963 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4965 | Py_INCREF(&PyDateTime_TZInfoType); |
| 4966 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4968 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 4969 | if (x == NULL) |
| 4970 | return NULL; |
| 4971 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 4972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4973 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 4974 | * pasting together 4 single years. |
| 4975 | */ |
| 4976 | assert(DI4Y == 4 * 365 + 1); |
| 4977 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4979 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 4980 | * get from pasting together 4 100-year cycles. |
| 4981 | */ |
| 4982 | assert(DI400Y == 4 * DI100Y + 1); |
| 4983 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4985 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 4986 | * pasting together 25 4-year cycles. |
| 4987 | */ |
| 4988 | assert(DI100Y == 25 * DI4Y - 1); |
| 4989 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 4991 | us_per_us = PyLong_FromLong(1); |
| 4992 | us_per_ms = PyLong_FromLong(1000); |
| 4993 | us_per_second = PyLong_FromLong(1000000); |
| 4994 | us_per_minute = PyLong_FromLong(60000000); |
| 4995 | seconds_per_day = PyLong_FromLong(24 * 3600); |
| 4996 | if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL || |
| 4997 | us_per_minute == NULL || seconds_per_day == NULL) |
| 4998 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 5000 | /* The rest are too big for 32-bit ints, but even |
| 5001 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5002 | */ |
| 5003 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5004 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5005 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5006 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5007 | return NULL; |
| 5008 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5009 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5010 | |
| 5011 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5012 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5013 | x.n = x stripped of its timezone -- its naive time. |
| 5014 | x.o = x.utcoffset(), and assuming that doesn't raise an exception or |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 5015 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5016 | x.d = x.dst(), and assuming that doesn't raise an exception or |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 5017 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5018 | x.s = x's standard offset, x.o - x.d |
| 5019 | |
| 5020 | Now some derived rules, where k is a duration (timedelta). |
| 5021 | |
| 5022 | 1. x.o = x.s + x.d |
| 5023 | This follows from the definition of x.s. |
| 5024 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5025 | 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] | 5026 | This is actually a requirement, an assumption we need to make about |
| 5027 | sane tzinfo classes. |
| 5028 | |
| 5029 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5030 | This is again a requirement for a sane tzinfo class. |
| 5031 | |
| 5032 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5033 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5034 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5035 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5036 | Again follows from how arithmetic is defined. |
| 5037 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5038 | 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] | 5039 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5040 | None when called). |
| 5041 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5042 | 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] | 5043 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5044 | |
| 5045 | By #3, we want |
| 5046 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5047 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5048 | |
| 5049 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5050 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5051 | becomes true; in effect, we want to solve [2] for k: |
| 5052 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5053 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5054 | |
| 5055 | By #1, this is the same as |
| 5056 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5057 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5058 | |
| 5059 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5060 | Substituting that into [3], |
| 5061 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5062 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5063 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5064 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5065 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5066 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5067 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5068 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5069 | very large, since all offset-returning methods return a duration of magnitude |
| 5070 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5071 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5072 | |
| 5073 | In any case, the new value is |
| 5074 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5075 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5076 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5077 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5078 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5079 | |
| 5080 | At this point, if |
| 5081 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5082 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5083 | |
| 5084 | 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] | 5085 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5086 | 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] | 5087 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5088 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5089 | 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] | 5090 | the only spelling that makes sense on the local wall clock. |
| 5091 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5092 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5093 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5094 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5095 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5096 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5097 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5098 | Now |
| 5099 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5100 | (y + y.s).n = by #5 |
| 5101 | y.n + y.s = since y.n = x.n |
| 5102 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5103 | y.s = z.s by #2 |
| 5104 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5105 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5106 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5107 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5108 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5109 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5110 | x.n - x.n - z.s + z.o = cancelling |
| 5111 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5112 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5113 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5114 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5115 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5116 | 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] | 5117 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5118 | 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] | 5119 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5120 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5121 | 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] | 5122 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5123 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5124 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5125 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5126 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5127 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5128 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5129 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5130 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5131 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5132 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5133 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5134 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5135 | the justifications for the kinds of substitutions we've done several times |
| 5136 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5137 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5138 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 5139 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5140 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5141 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5142 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5143 | - z.o + z'.o = #1 twice |
| 5144 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5145 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5146 | |
| 5147 | 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] | 5148 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5149 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5150 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5151 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 5152 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 5153 | would have to change the result dst() returns: we start in DST, and moving |
| 5154 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5155 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5156 | There isn't a sane case where this can happen. The closest it gets is at |
| 5157 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 5158 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 5159 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 5160 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 5161 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 5162 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 5163 | standard time. Since that's what the local clock *does*, we want to map both |
| 5164 | 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] | 5165 | in local time, but so it goes -- it's the way the local clock works. |
| 5166 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5167 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 5168 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 5169 | 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] | 5170 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 5171 | |
| 5172 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 5173 | 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] | 5174 | 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] | 5175 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 5176 | but the reasoning doesn't depend on the example -- it depends on there being |
| 5177 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5178 | z' must be in standard time, and is the spelling we want in this case. |
| 5179 | |
| 5180 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 5181 | concerned (because it takes z' as being in standard time rather than the |
| 5182 | daylight time we intend here), but returning it gives the real-life "local |
| 5183 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 5184 | tz. |
| 5185 | |
| 5186 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 5187 | the 1:MM standard time spelling we want. |
| 5188 | |
| 5189 | So how can this break? One of the assumptions must be violated. Two |
| 5190 | possibilities: |
| 5191 | |
| 5192 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 5193 | time zone. This isn't true if, for political reasons or continental drift, |
| 5194 | a region decides to change its base offset from UTC. |
| 5195 | |
| 5196 | 2) There may be versions of "double daylight" time where the tail end of |
| 5197 | the analysis gives up a step too early. I haven't thought about that |
| 5198 | enough to say. |
| 5199 | |
| 5200 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 5201 | "almost all" time zones: so long as the standard offset is invariant, it |
| 5202 | doesn't matter if daylight time transition points change from year to year, or |
| 5203 | if daylight time is skipped in some years; it doesn't matter how large or |
| 5204 | small dst() may get within its bounds; and it doesn't even matter if some |
| 5205 | perverse time zone returns a negative dst()). So a breaking case must be |
| 5206 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5207 | --------------------------------------------------------------------------- */ |