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