Mercurial > cpython-lru_cache-weakref
annotate Modules/datetimemodule.c @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 1c964ed35ef9 |
| children | 23f467151756 a6981f4a74a1 |
| rev | line source |
|---|---|
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1 /* C implementation for the date/time type documented at |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
5 #define PY_SSIZE_T_CLEAN |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
6 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 #include "Python.h" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
8 #include "modsupport.h" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
9 #include "structmember.h" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
10 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
11 #include <time.h> |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
12 |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
13 #include "timefuncs.h" |
|
32393
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
14 |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
15 /* Differentiate between building the core module and building extension |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
16 * modules. |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
17 */ |
|
41561
4f801a758936
Complete revamp of PCBuild8 directory. Use subdirectories for each project under the main pcbuild solution. Now make extensive use of property sheets to simplify project configuration. x64 build fully supported, and the process for building PGO version (Profiler Guided Optimization) simplified. All projects are now present, except _ssl, which needs to be reimplemented. Also, some of the projects that require external libraries need extra work to fully compile on x64.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41026
diff
changeset
|
18 #ifndef Py_BUILD_CORE |
|
32393
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
19 #define Py_BUILD_CORE |
|
41561
4f801a758936
Complete revamp of PCBuild8 directory. Use subdirectories for each project under the main pcbuild solution. Now make extensive use of property sheets to simplify project configuration. x64 build fully supported, and the process for building PGO version (Profiler Guided Optimization) simplified. All projects are now present, except _ssl, which needs to be reimplemented. Also, some of the projects that require external libraries need extra work to fully compile on x64.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41026
diff
changeset
|
20 #endif |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
21 #include "datetime.h" |
|
32393
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
22 #undef Py_BUILD_CORE |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
23 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
24 /* We require that C int be at least 32 bits, and use int virtually |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
25 * everywhere. In just a few cases we use a temp long, where a Python |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
26 * API returns a C long. In such cases, we have to ensure that the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
27 * final result fits in a C int (this can be an issue on 64-bit boxes). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
28 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
29 #if SIZEOF_INT < 4 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
30 # error "datetime.c requires that C int have at least 32 bits" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
31 #endif |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
32 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
33 #define MINYEAR 1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 #define MAXYEAR 9999 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
35 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
36 /* Nine decimal digits is easy to communicate, and leaves enough room |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
37 * so that two delta days can be added w/o fear of overflowing a signed |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
38 * 32-bit int, and with plenty of room left over to absorb any possible |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
39 * carries from adding seconds. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
40 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
41 #define MAX_DELTA_DAYS 999999999 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
42 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
43 /* Rename the long macros in datetime.h to more reasonable short names. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
44 #define GET_YEAR PyDateTime_GET_YEAR |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
45 #define GET_MONTH PyDateTime_GET_MONTH |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
46 #define GET_DAY PyDateTime_GET_DAY |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
47 #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
48 #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
49 #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
50 #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
51 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
52 /* Date accessors for date and datetime. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
53 #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
54 ((o)->data[1] = ((v) & 0x00ff))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
55 #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
56 #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
57 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
58 /* Date/Time accessors for datetime. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
59 #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
60 #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
61 #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
62 #define DATE_SET_MICROSECOND(o, v) \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
63 (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
64 ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
65 ((o)->data[9] = ((v) & 0x0000ff))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
66 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
67 /* Time accessors for time. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
68 #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
69 #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
70 #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
71 #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
72 #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
73 #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
74 #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
75 #define TIME_SET_MICROSECOND(o, v) \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
76 (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
77 ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
78 ((o)->data[5] = ((v) & 0x0000ff))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
79 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
80 /* Delta accessors for timedelta. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
81 #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
82 #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
83 #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
84 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
85 #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
86 #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
87 #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
88 |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
89 /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
|
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
90 * p->hastzinfo. |
|
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
91 */ |
|
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
92 #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
|
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
93 |
|
31760
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
31608
diff
changeset
|
94 /* M is a char or int claiming to be a valid month. The macro is equivalent |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
31608
diff
changeset
|
95 * to the two-sided Python test |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
31608
diff
changeset
|
96 * 1 <= M <= 12 |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
31608
diff
changeset
|
97 */ |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
31608
diff
changeset
|
98 #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
31608
diff
changeset
|
99 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
100 /* Forward declarations. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
101 static PyTypeObject PyDateTime_DateType; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
102 static PyTypeObject PyDateTime_DateTimeType; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
103 static PyTypeObject PyDateTime_DeltaType; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
104 static PyTypeObject PyDateTime_TimeType; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
105 static PyTypeObject PyDateTime_TZInfoType; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
106 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
107 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
108 * Math utilities. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
109 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
110 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
111 /* k = i+j overflows iff k differs in sign from both inputs, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
112 * iff k^i has sign bit set and k^j has sign bit set, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
113 * iff (k^i)&(k^j) has sign bit set. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
114 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
115 #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
116 ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
117 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
118 /* Compute Python divmod(x, y), returning the quotient and storing the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
119 * remainder into *r. The quotient is the floor of x/y, and that's |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
120 * the real point of this. C will probably truncate instead (C99 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
121 * requires truncation; C89 left it implementation-defined). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
122 * Simplification: we *require* that y > 0 here. That's appropriate |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
123 * for all the uses made of it. This simplifies the code and makes |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
124 * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
125 * overflow case). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
126 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
127 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
128 divmod(int x, int y, int *r) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
129 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
130 int quo; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
131 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
132 assert(y > 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
133 quo = x / y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
134 *r = x - quo * y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
135 if (*r < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
136 --quo; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
137 *r += y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
138 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
139 assert(0 <= *r && *r < y); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
140 return quo; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
141 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
142 |
|
27029
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
143 /* Round a double to the nearest long. |x| must be small enough to fit |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
144 * in a C long; this is not checked. |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
145 */ |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
146 static long |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
147 round_to_long(double x) |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
148 { |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
149 if (x >= 0.0) |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
150 x = floor(x + 0.5); |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
151 else |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
152 x = ceil(x - 0.5); |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
153 return (long)x; |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
154 } |
|
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
155 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
156 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
157 * General calendrical helper functions |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
158 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
159 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
160 /* For each month ordinal in 1..12, the number of days in that month, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
161 * and the number of days before that month in the same year. These |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
162 * are correct for non-leap years only. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
163 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
164 static int _days_in_month[] = { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
165 0, /* unused; this vector uses 1-based indexing */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
166 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
167 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
168 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
169 static int _days_before_month[] = { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
170 0, /* unused; this vector uses 1-based indexing */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
171 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
172 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
173 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
174 /* year -> 1 if leap year, else 0. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
175 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
176 is_leap(int year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
177 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
178 /* Cast year to unsigned. The result is the same either way, but |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
179 * C can generate faster code for unsigned mod than for signed |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
180 * mod (especially for % 4 -- a good compiler should just grab |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
181 * the last 2 bits when the LHS is unsigned). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
182 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
183 const unsigned int ayear = (unsigned int)year; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
184 return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
185 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
186 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
187 /* year, month -> number of days in that month in that year */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
188 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
189 days_in_month(int year, int month) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
190 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
191 assert(month >= 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
192 assert(month <= 12); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
193 if (month == 2 && is_leap(year)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
194 return 29; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
195 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
196 return _days_in_month[month]; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
197 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
198 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
199 /* year, month -> number of days in year preceeding first day of month */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
200 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
201 days_before_month(int year, int month) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
202 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
203 int days; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
204 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
205 assert(month >= 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
206 assert(month <= 12); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
207 days = _days_before_month[month]; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
208 if (month > 2 && is_leap(year)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
209 ++days; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
210 return days; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
211 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
213 /* year -> number of days before January 1st of year. Remember that we |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
214 * start with year 1, so days_before_year(1) == 0. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
215 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
216 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
217 days_before_year(int year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
218 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
219 int y = year - 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
220 /* This is incorrect if year <= 0; we really want the floor |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
221 * here. But so long as MINYEAR is 1, the smallest year this |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
222 * can see is 0 (this can happen in some normalization endcases), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
223 * so we'll just special-case that. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
224 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
225 assert (year >= 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
226 if (y >= 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
227 return y*365 + y/4 - y/100 + y/400; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
228 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
229 assert(y == -1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
230 return -366; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
231 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
232 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
233 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
234 /* Number of days in 4, 100, and 400 year cycles. That these have |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
235 * the correct values is asserted in the module init function. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
236 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
237 #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
238 #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
239 #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
240 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
241 /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
242 static void |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
243 ord_to_ymd(int ordinal, int *year, int *month, int *day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
244 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
245 int n, n1, n4, n100, n400, leapyear, preceding; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
246 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
247 /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
248 * leap years repeats exactly every 400 years. The basic strategy is |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
249 * to find the closest 400-year boundary at or before ordinal, then |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
250 * work with the offset from that boundary to ordinal. Life is much |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
251 * clearer if we subtract 1 from ordinal first -- then the values |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
252 * of ordinal at 400-year boundaries are exactly those divisible |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
253 * by DI400Y: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
254 * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
255 * D M Y n n-1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
256 * -- --- ---- ---------- ---------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
257 * 31 Dec -400 -DI400Y -DI400Y -1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
258 * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
259 * ... |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
260 * 30 Dec 000 -1 -2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
261 * 31 Dec 000 0 -1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
262 * 1 Jan 001 1 0 400-year boundary |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
263 * 2 Jan 001 2 1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
264 * 3 Jan 001 3 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
265 * ... |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
266 * 31 Dec 400 DI400Y DI400Y -1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
267 * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
268 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
269 assert(ordinal >= 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
270 --ordinal; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
271 n400 = ordinal / DI400Y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
272 n = ordinal % DI400Y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
273 *year = n400 * 400 + 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
274 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
275 /* Now n is the (non-negative) offset, in days, from January 1 of |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
276 * year, to the desired date. Now compute how many 100-year cycles |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
277 * precede n. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
278 * Note that it's possible for n100 to equal 4! In that case 4 full |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
279 * 100-year cycles precede the desired day, which implies the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
280 * desired day is December 31 at the end of a 400-year cycle. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
281 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
282 n100 = n / DI100Y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
283 n = n % DI100Y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
284 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
285 /* Now compute how many 4-year cycles precede it. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
286 n4 = n / DI4Y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
287 n = n % DI4Y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
288 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
289 /* And now how many single years. Again n1 can be 4, and again |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
290 * meaning that the desired day is December 31 at the end of the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
291 * 4-year cycle. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
292 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
293 n1 = n / 365; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
294 n = n % 365; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
295 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
296 *year += n100 * 100 + n4 * 4 + n1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
297 if (n1 == 4 || n100 == 4) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
298 assert(n == 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
299 *year -= 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
300 *month = 12; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
301 *day = 31; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
302 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
303 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
304 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
305 /* Now the year is correct, and n is the offset from January 1. We |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
306 * find the month via an estimate that's either exact or one too |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
307 * large. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
308 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
309 leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
310 assert(leapyear == is_leap(*year)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
311 *month = (n + 50) >> 5; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
312 preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
313 if (preceding > n) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
314 /* estimate is too large */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
315 *month -= 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
316 preceding -= days_in_month(*year, *month); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
317 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
318 n -= preceding; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
319 assert(0 <= n); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
320 assert(n < days_in_month(*year, *month)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
321 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
322 *day = n + 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
323 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
324 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
325 /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
326 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
327 ymd_to_ord(int year, int month, int day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
328 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
329 return days_before_year(year) + days_before_month(year, month) + day; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
330 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
331 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
332 /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
333 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
334 weekday(int year, int month, int day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
335 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
336 return (ymd_to_ord(year, month, day) + 6) % 7; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
337 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
338 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
339 /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
340 * first calendar week containing a Thursday. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
341 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
342 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
343 iso_week1_monday(int year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
344 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
345 int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
346 /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
347 int first_weekday = (first_day + 6) % 7; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
348 /* ordinal of closest Monday at or before 1/1 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
349 int week1_monday = first_day - first_weekday; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
350 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
351 if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
352 week1_monday += 7; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
353 return week1_monday; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
354 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
355 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
356 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
357 * Range checkers. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
358 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
359 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
360 /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
361 * If not, raise OverflowError and return -1. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
362 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
363 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
364 check_delta_day_range(int days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
365 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
366 if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
367 return 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
368 PyErr_Format(PyExc_OverflowError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
369 "days=%d; must have magnitude <= %d", |
|
26675
bc9056d847ef
Fix GCC warnings. It turns out two out of three pointed to real bugs!
Guido van Rossum <guido@python.org>
parents:
26673
diff
changeset
|
370 days, MAX_DELTA_DAYS); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
371 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
372 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
373 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
374 /* Check that date arguments are in range. Return 0 if they are. If they |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
375 * aren't, raise ValueError and return -1. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
376 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
377 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
378 check_date_args(int year, int month, int day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
379 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
380 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
381 if (year < MINYEAR || year > MAXYEAR) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
382 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
383 "year is out of range"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
384 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
385 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
386 if (month < 1 || month > 12) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
387 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
388 "month must be in 1..12"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
389 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
390 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
391 if (day < 1 || day > days_in_month(year, month)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
392 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
393 "day is out of range for month"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
394 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
395 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
396 return 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
397 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
398 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
399 /* Check that time arguments are in range. Return 0 if they are. If they |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
400 * aren't, raise ValueError and return -1. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
401 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
402 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
403 check_time_args(int h, int m, int s, int us) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
404 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
405 if (h < 0 || h > 23) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
406 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
407 "hour must be in 0..23"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
408 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
409 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
410 if (m < 0 || m > 59) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
411 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
412 "minute must be in 0..59"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
413 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
414 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
415 if (s < 0 || s > 59) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
416 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
417 "second must be in 0..59"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
418 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
419 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
420 if (us < 0 || us > 999999) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
421 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
422 "microsecond must be in 0..999999"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
423 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
424 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
425 return 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
426 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
427 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
428 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
429 * Normalization utilities. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
430 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
431 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
432 /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
433 * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
434 * at least factor, enough of *lo is converted into "hi" units so that |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
435 * 0 <= *lo < factor. The input values must be such that int overflow |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
436 * is impossible. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
437 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
438 static void |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
439 normalize_pair(int *hi, int *lo, int factor) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
440 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
441 assert(factor > 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
442 assert(lo != hi); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
443 if (*lo < 0 || *lo >= factor) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
444 const int num_hi = divmod(*lo, factor, lo); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
445 const int new_hi = *hi + num_hi; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
446 assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
447 *hi = new_hi; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
448 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
449 assert(0 <= *lo && *lo < factor); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
450 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
451 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
452 /* Fiddle days (d), seconds (s), and microseconds (us) so that |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
453 * 0 <= *s < 24*3600 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
454 * 0 <= *us < 1000000 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
455 * The input values must be such that the internals don't overflow. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
456 * The way this routine is used, we don't get close. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
457 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
458 static void |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
459 normalize_d_s_us(int *d, int *s, int *us) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
460 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
461 if (*us < 0 || *us >= 1000000) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
462 normalize_pair(s, us, 1000000); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
463 /* |s| can't be bigger than about |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
464 * |original s| + |original us|/1000000 now. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
465 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
466 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
467 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
468 if (*s < 0 || *s >= 24*3600) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
469 normalize_pair(d, s, 24*3600); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
470 /* |d| can't be bigger than about |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
471 * |original d| + |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
472 * (|original s| + |original us|/1000000) / (24*3600) now. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
473 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
474 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
475 assert(0 <= *s && *s < 24*3600); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
476 assert(0 <= *us && *us < 1000000); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
477 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
478 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
479 /* Fiddle years (y), months (m), and days (d) so that |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
480 * 1 <= *m <= 12 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
481 * 1 <= *d <= days_in_month(*y, *m) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
482 * The input values must be such that the internals don't overflow. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
483 * The way this routine is used, we don't get close. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
484 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
485 static void |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
486 normalize_y_m_d(int *y, int *m, int *d) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
487 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
488 int dim; /* # of days in month */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
489 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
490 /* This gets muddy: the proper range for day can't be determined |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
491 * without knowing the correct month and year, but if day is, e.g., |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
492 * plus or minus a million, the current month and year values make |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
493 * no sense (and may also be out of bounds themselves). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
494 * Saying 12 months == 1 year should be non-controversial. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
495 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
496 if (*m < 1 || *m > 12) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
497 --*m; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
498 normalize_pair(y, m, 12); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
499 ++*m; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
500 /* |y| can't be bigger than about |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
501 * |original y| + |original m|/12 now. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
502 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
503 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
504 assert(1 <= *m && *m <= 12); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
505 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
506 /* Now only day can be out of bounds (year may also be out of bounds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
507 * for a datetime object, but we don't care about that here). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
508 * If day is out of bounds, what to do is arguable, but at least the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
509 * method here is principled and explainable. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
510 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
511 dim = days_in_month(*y, *m); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
512 if (*d < 1 || *d > dim) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
513 /* Move day-1 days from the first of the month. First try to |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
514 * get off cheap if we're only one day out of range |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
515 * (adjustments for timezone alone can't be worse than that). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
516 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
517 if (*d == 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
518 --*m; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
519 if (*m > 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
520 *d = days_in_month(*y, *m); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
521 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
522 --*y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
523 *m = 12; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
524 *d = 31; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
525 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
526 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
527 else if (*d == dim + 1) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
528 /* move forward a day */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
529 ++*m; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
530 *d = 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
531 if (*m > 12) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
532 *m = 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
533 ++*y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
534 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
535 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
536 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
537 int ordinal = ymd_to_ord(*y, *m, 1) + |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
538 *d - 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
539 ord_to_ymd(ordinal, y, m, d); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
540 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
541 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
542 assert(*m > 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
543 assert(*d > 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
544 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
545 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
546 /* Fiddle out-of-bounds months and days so that the result makes some kind |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
547 * of sense. The parameters are both inputs and outputs. Returns < 0 on |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
548 * failure, where failure means the adjusted year is out of bounds. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
549 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
550 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
551 normalize_date(int *year, int *month, int *day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
552 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
553 int result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
554 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
555 normalize_y_m_d(year, month, day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
556 if (MINYEAR <= *year && *year <= MAXYEAR) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
557 result = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
558 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
559 PyErr_SetString(PyExc_OverflowError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
560 "date value out of range"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
561 result = -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
562 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
563 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
564 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
565 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
566 /* Force all the datetime fields into range. The parameters are both |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
567 * inputs and outputs. Returns < 0 on error. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
568 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
569 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
570 normalize_datetime(int *year, int *month, int *day, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
571 int *hour, int *minute, int *second, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
572 int *microsecond) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
573 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
574 normalize_pair(second, microsecond, 1000000); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
575 normalize_pair(minute, second, 60); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
576 normalize_pair(hour, minute, 60); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
577 normalize_pair(day, hour, 24); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
578 return normalize_date(year, month, day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
579 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
580 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
581 /* --------------------------------------------------------------------------- |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
582 * Basic object allocation: tp_alloc implementations. These allocate |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
583 * Python objects of the right size and type, and do the Python object- |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
584 * initialization bit. If there's not enough memory, they return NULL after |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
585 * setting MemoryError. All data members remain uninitialized trash. |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
586 * |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
587 * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
|
29161
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
588 * member is needed. This is ugly, imprecise, and possibly insecure. |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
589 * tp_basicsize for the time and datetime types is set to the size of the |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
590 * struct that has room for the tzinfo member, so subclasses in Python will |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
591 * allocate enough space for a tzinfo member whether or not one is actually |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
592 * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
593 * part is that PyType_GenericAlloc() (which subclasses in Python end up |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
594 * using) just happens today to effectively ignore the nitems argument |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
595 * when tp_itemsize is 0, which it is for these type objects. If that |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
596 * changes, perhaps the callers of tp_alloc slots in this file should |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
597 * be changed to force a 0 nitems argument unless the type being allocated |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
598 * is a base type implemented in this file (so that tp_alloc is time_alloc |
|
1d4ec52b316c
Added more words about the abuse of the tp_alloc nitems argument
Tim Peters <tim.peters@gmail.com>
parents:
29153
diff
changeset
|
599 * or datetime_alloc below, which know about the nitems abuse). |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
600 */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
601 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
602 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
603 time_alloc(PyTypeObject *type, Py_ssize_t aware) |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
604 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
605 PyObject *self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
606 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
607 self = (PyObject *) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
608 PyObject_MALLOC(aware ? |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
609 sizeof(PyDateTime_Time) : |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
610 sizeof(_PyDateTime_BaseTime)); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
611 if (self == NULL) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
612 return (PyObject *)PyErr_NoMemory(); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
613 PyObject_INIT(self, type); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
614 return self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
615 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
616 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
617 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
618 datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
619 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
620 PyObject *self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
621 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
622 self = (PyObject *) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
623 PyObject_MALLOC(aware ? |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
624 sizeof(PyDateTime_DateTime) : |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
625 sizeof(_PyDateTime_BaseDateTime)); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
626 if (self == NULL) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
627 return (PyObject *)PyErr_NoMemory(); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
628 PyObject_INIT(self, type); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
629 return self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
630 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
631 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
632 /* --------------------------------------------------------------------------- |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
633 * Helpers for setting object fields. These work on pointers to the |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
634 * appropriate base class. |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
635 */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
636 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
637 /* For date and datetime. */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
638 static void |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
639 set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
640 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
641 self->hashcode = -1; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
642 SET_YEAR(self, y); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
643 SET_MONTH(self, m); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
644 SET_DAY(self, d); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
645 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
646 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
647 /* --------------------------------------------------------------------------- |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
648 * Create various objects, mostly without range checking. |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
649 */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
650 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
651 /* Create a date instance with no range checking. */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
652 static PyObject * |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
653 new_date_ex(int year, int month, int day, PyTypeObject *type) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
654 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
655 PyDateTime_Date *self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
656 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
657 self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
658 if (self != NULL) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
659 set_date_fields(self, year, month, day); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
660 return (PyObject *) self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
661 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
662 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
663 #define new_date(year, month, day) \ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
664 new_date_ex(year, month, day, &PyDateTime_DateType) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
665 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
666 /* Create a datetime instance with no range checking. */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
667 static PyObject * |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
668 new_datetime_ex(int year, int month, int day, int hour, int minute, |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
669 int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
670 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
671 PyDateTime_DateTime *self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
672 char aware = tzinfo != Py_None; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
673 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
674 self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
675 if (self != NULL) { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
676 self->hastzinfo = aware; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
677 set_date_fields((PyDateTime_Date *)self, year, month, day); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
678 DATE_SET_HOUR(self, hour); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
679 DATE_SET_MINUTE(self, minute); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
680 DATE_SET_SECOND(self, second); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
681 DATE_SET_MICROSECOND(self, usecond); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
682 if (aware) { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
683 Py_INCREF(tzinfo); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
684 self->tzinfo = tzinfo; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
685 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
686 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
687 return (PyObject *)self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
688 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
689 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
690 #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
691 new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
692 &PyDateTime_DateTimeType) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
693 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
694 /* Create a time instance with no range checking. */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
695 static PyObject * |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
696 new_time_ex(int hour, int minute, int second, int usecond, |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
697 PyObject *tzinfo, PyTypeObject *type) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
698 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
699 PyDateTime_Time *self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
700 char aware = tzinfo != Py_None; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
701 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
702 self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
703 if (self != NULL) { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
704 self->hastzinfo = aware; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
705 self->hashcode = -1; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
706 TIME_SET_HOUR(self, hour); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
707 TIME_SET_MINUTE(self, minute); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
708 TIME_SET_SECOND(self, second); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
709 TIME_SET_MICROSECOND(self, usecond); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
710 if (aware) { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
711 Py_INCREF(tzinfo); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
712 self->tzinfo = tzinfo; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
713 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
714 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
715 return (PyObject *)self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
716 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
717 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
718 #define new_time(hh, mm, ss, us, tzinfo) \ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
719 new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
720 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
721 /* Create a timedelta instance. Normalize the members iff normalize is |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
722 * true. Passing false is a speed optimization, if you know for sure |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
723 * that seconds and microseconds are already in their proper ranges. In any |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
724 * case, raises OverflowError and returns NULL if the normalized days is out |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
725 * of range). |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
726 */ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
727 static PyObject * |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
728 new_delta_ex(int days, int seconds, int microseconds, int normalize, |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
729 PyTypeObject *type) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
730 { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
731 PyDateTime_Delta *self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
732 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
733 if (normalize) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
734 normalize_d_s_us(&days, &seconds, µseconds); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
735 assert(0 <= seconds && seconds < 24*3600); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
736 assert(0 <= microseconds && microseconds < 1000000); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
737 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
738 if (check_delta_day_range(days) < 0) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
739 return NULL; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
740 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
741 self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
742 if (self != NULL) { |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
743 self->hashcode = -1; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
744 SET_TD_DAYS(self, days); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
745 SET_TD_SECONDS(self, seconds); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
746 SET_TD_MICROSECONDS(self, microseconds); |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
747 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
748 return (PyObject *) self; |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
749 } |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
750 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
751 #define new_delta(d, s, us, normalize) \ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
752 new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
753 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
754 /* --------------------------------------------------------------------------- |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
755 * tzinfo helpers. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
756 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
757 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
758 /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
759 * raise TypeError and return -1. |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
760 */ |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
761 static int |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
762 check_tzinfo_subclass(PyObject *p) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
763 { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
764 if (p == Py_None || PyTZInfo_Check(p)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
765 return 0; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
766 PyErr_Format(PyExc_TypeError, |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
767 "tzinfo argument must be None or of a tzinfo subclass, " |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
768 "not type '%s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
769 Py_TYPE(p)->tp_name); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
770 return -1; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
771 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
772 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
773 /* Return tzinfo.methname(tzinfoarg), without any checking of results. |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
774 * If tzinfo is None, returns None. |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
775 */ |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
776 static PyObject * |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
777 call_tzinfo_method(PyObject *tzinfo, char *methname, PyObject *tzinfoarg) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
778 { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
779 PyObject *result; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
780 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
781 assert(tzinfo && methname && tzinfoarg); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
782 assert(check_tzinfo_subclass(tzinfo) >= 0); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
783 if (tzinfo == Py_None) { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
784 result = Py_None; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
785 Py_INCREF(result); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
786 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
787 else |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
788 result = PyObject_CallMethod(tzinfo, methname, "O", tzinfoarg); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
789 return result; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
790 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
791 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
792 /* If self has a tzinfo member, return a BORROWED reference to it. Else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
793 * return NULL, which is NOT AN ERROR. There are no error returns here, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
794 * and the caller must not decref the result. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
795 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
796 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
797 get_tzinfo_member(PyObject *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
798 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
799 PyObject *tzinfo = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
800 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
801 if (PyDateTime_Check(self) && HASTZINFO(self)) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
802 tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
803 else if (PyTime_Check(self) && HASTZINFO(self)) |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
804 tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
805 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
806 return tzinfo; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
807 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
808 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
809 /* Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
810 * result. tzinfo must be an instance of the tzinfo class. If the method |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
811 * returns None, this returns 0 and sets *none to 1. If the method doesn't |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
812 * return None or timedelta, TypeError is raised and this returns -1. If it |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
813 * returnsa timedelta and the value is out of range or isn't a whole number |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
814 * of minutes, ValueError is raised and this returns -1. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
815 * Else *none is set to 0 and the integer method result is returned. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
816 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
817 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
818 call_utc_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
819 int *none) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
820 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
821 PyObject *u; |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
822 int result = -1; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
823 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
824 assert(tzinfo != NULL); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
825 assert(PyTZInfo_Check(tzinfo)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
826 assert(tzinfoarg != NULL); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
827 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
828 *none = 0; |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
829 u = call_tzinfo_method(tzinfo, name, tzinfoarg); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
830 if (u == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
831 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
832 |
|
26751
65acec9cbe69
call_utc_tzinfo_method(): Got rid of the label and the gotos.
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
833 else if (u == Py_None) { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
834 result = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
835 *none = 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
836 } |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
837 else if (PyDelta_Check(u)) { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
838 const int days = GET_TD_DAYS(u); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
839 if (days < -1 || days > 0) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
840 result = 24*60; /* trigger ValueError below */ |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
841 else { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
842 /* next line can't overflow because we know days |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
843 * is -1 or 0 now |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
844 */ |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
845 int ss = days * 24 * 3600 + GET_TD_SECONDS(u); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
846 result = divmod(ss, 60, &ss); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
847 if (ss || GET_TD_MICROSECONDS(u)) { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
848 PyErr_Format(PyExc_ValueError, |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
849 "tzinfo.%s() must return a " |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
850 "whole number of minutes", |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
851 name); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
852 result = -1; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
853 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
854 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
855 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
856 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
857 PyErr_Format(PyExc_TypeError, |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
858 "tzinfo.%s() must return None or " |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
859 "timedelta, not '%s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
860 name, Py_TYPE(u)->tp_name); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
861 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
862 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
863 Py_DECREF(u); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
864 if (result < -1439 || result > 1439) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
865 PyErr_Format(PyExc_ValueError, |
| 27073 | 866 "tzinfo.%s() returned %d; must be in " |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
867 "-1439 .. 1439", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
868 name, result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
869 result = -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
870 } |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
871 return result; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
872 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
873 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
874 /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
875 * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
876 * returns None, call_utcoffset returns 0 and sets *none to 1. If uctoffset() |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
877 * doesn't return None or timedelta, TypeError is raised and this returns -1. |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
878 * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
879 * # of minutes), ValueError is raised and this returns -1. Else *none is |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
880 * set to 0 and the offset is returned (as int # of minutes east of UTC). |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
881 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
882 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
883 call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg, int *none) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
884 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
885 return call_utc_tzinfo_method(tzinfo, "utcoffset", tzinfoarg, none); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
886 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
887 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
888 /* Call tzinfo.name(tzinfoarg), and return the offset as a timedelta or None. |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
889 */ |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
890 static PyObject * |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
891 offset_as_timedelta(PyObject *tzinfo, char *name, PyObject *tzinfoarg) { |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
892 PyObject *result; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
893 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
894 assert(tzinfo && name && tzinfoarg); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
895 if (tzinfo == Py_None) { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
896 result = Py_None; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
897 Py_INCREF(result); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
898 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
899 else { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
900 int none; |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
901 int offset = call_utc_tzinfo_method(tzinfo, name, tzinfoarg, |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
902 &none); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
903 if (offset < 0 && PyErr_Occurred()) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
904 return NULL; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
905 if (none) { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
906 result = Py_None; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
907 Py_INCREF(result); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
908 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
909 else |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
910 result = new_delta(0, offset * 60, 0, 1); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
911 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
912 return result; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
913 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
914 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
915 /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
916 * result. tzinfo must be an instance of the tzinfo class. If dst() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
917 * returns None, call_dst returns 0 and sets *none to 1. If dst() |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
918 & doesn't return None or timedelta, TypeError is raised and this |
|
30722
651fc8d98d99
Fix a bunch of typos in documentation, docstrings and comments.
Walter Dörwald <walter@livinglogic.de>
parents:
30683
diff
changeset
|
919 * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
920 * ValueError is raised and this returns -1. Else *none is set to 0 and |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
921 * the offset is returned (as an int # of minutes east of UTC). |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
922 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
923 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
924 call_dst(PyObject *tzinfo, PyObject *tzinfoarg, int *none) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
925 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
926 return call_utc_tzinfo_method(tzinfo, "dst", tzinfoarg, none); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
927 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
928 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
929 /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
930 * an instance of the tzinfo class or None. If tzinfo isn't None, and |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
931 * tzname() doesn't return None or a string, TypeError is raised and this |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
932 * returns NULL. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
933 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
934 static PyObject * |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
935 call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
936 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
937 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
938 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
939 assert(tzinfo != NULL); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
940 assert(check_tzinfo_subclass(tzinfo) >= 0); |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
941 assert(tzinfoarg != NULL); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
942 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
943 if (tzinfo == Py_None) { |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
944 result = Py_None; |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
945 Py_INCREF(result); |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
946 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
947 else |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
948 result = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
949 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
950 if (result != NULL && result != Py_None && ! PyString_Check(result)) { |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
951 PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
952 "return None or a string, not '%s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
953 Py_TYPE(result)->tp_name); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
954 Py_DECREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
955 result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
956 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
957 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
958 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
959 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
960 typedef enum { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
961 /* an exception has been set; the caller should pass it on */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
962 OFFSET_ERROR, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
963 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
964 /* type isn't date, datetime, or time subclass */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
965 OFFSET_UNKNOWN, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
966 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
967 /* date, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
968 * datetime with !hastzinfo |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
969 * datetime with None tzinfo, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
970 * datetime where utcoffset() returns None |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
971 * time with !hastzinfo |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
972 * time with None tzinfo, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
973 * time where utcoffset() returns None |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
974 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
975 OFFSET_NAIVE, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
976 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
977 /* time or datetime where utcoffset() doesn't return None */ |
|
36599
c58e79b2ca05
Remove two instances of trailing commas. Resolves patch #1209781.
Georg Brandl <georg@python.org>
parents:
36501
diff
changeset
|
978 OFFSET_AWARE |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
979 } naivety; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
980 |
|
26744
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
981 /* Classify an object as to whether it's naive or offset-aware. See |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
982 * the "naivety" typedef for details. If the type is aware, *offset is set |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
983 * to minutes east of UTC (as returned by the tzinfo.utcoffset() method). |
|
26744
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
984 * If the type is offset-naive (or unknown, or error), *offset is set to 0. |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
985 * tzinfoarg is the argument to pass to the tzinfo.utcoffset() method. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
986 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
987 static naivety |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
988 classify_utcoffset(PyObject *op, PyObject *tzinfoarg, int *offset) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
989 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
990 int none; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
991 PyObject *tzinfo; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
992 |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
993 assert(tzinfoarg != NULL); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
994 *offset = 0; |
|
26744
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
995 tzinfo = get_tzinfo_member(op); /* NULL means no tzinfo, not error */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
996 if (tzinfo == Py_None) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
997 return OFFSET_NAIVE; |
|
26744
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
998 if (tzinfo == NULL) { |
|
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
999 /* note that a datetime passes the PyDate_Check test */ |
|
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1000 return (PyTime_Check(op) || PyDate_Check(op)) ? |
|
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1001 OFFSET_NAIVE : OFFSET_UNKNOWN; |
|
b0acf5de3421
classify_object(): Renamed more meaningfully, to classify_utcoffset().
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1002 } |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
1003 *offset = call_utcoffset(tzinfo, tzinfoarg, &none); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1004 if (*offset == -1 && PyErr_Occurred()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1005 return OFFSET_ERROR; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1006 return none ? OFFSET_NAIVE : OFFSET_AWARE; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1007 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1008 |
|
26808
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1009 /* Classify two objects as to whether they're naive or offset-aware. |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1010 * This isn't quite the same as calling classify_utcoffset() twice: for |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1011 * binary operations (comparison and subtraction), we generally want to |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1012 * ignore the tzinfo members if they're identical. This is by design, |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1013 * so that results match "naive" expectations when mixing objects from a |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1014 * single timezone. So in that case, this sets both offsets to 0 and |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1015 * both naiveties to OFFSET_NAIVE. |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1016 * The function returns 0 if everything's OK, and -1 on error. |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1017 */ |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1018 static int |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1019 classify_two_utcoffsets(PyObject *o1, int *offset1, naivety *n1, |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
1020 PyObject *tzinfoarg1, |
|
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
1021 PyObject *o2, int *offset2, naivety *n2, |
|
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
1022 PyObject *tzinfoarg2) |
|
26808
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1023 { |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1024 if (get_tzinfo_member(o1) == get_tzinfo_member(o2)) { |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1025 *offset1 = *offset2 = 0; |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1026 *n1 = *n2 = OFFSET_NAIVE; |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1027 } |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1028 else { |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
1029 *n1 = classify_utcoffset(o1, tzinfoarg1, offset1); |
|
26808
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1030 if (*n1 == OFFSET_ERROR) |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1031 return -1; |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
1032 *n2 = classify_utcoffset(o2, tzinfoarg2, offset2); |
|
26808
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1033 if (*n2 == OFFSET_ERROR) |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1034 return -1; |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1035 } |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1036 return 0; |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1037 } |
|
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
1038 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1039 /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1040 * stuff |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1041 * ", tzinfo=" + repr(tzinfo) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1042 * before the closing ")". |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1043 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1044 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1045 append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1046 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1047 PyObject *temp; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1048 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1049 assert(PyString_Check(repr)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1050 assert(tzinfo); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1051 if (tzinfo == Py_None) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1052 return repr; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1053 /* Get rid of the trailing ')'. */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1054 assert(PyString_AsString(repr)[PyString_Size(repr)-1] == ')'); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1055 temp = PyString_FromStringAndSize(PyString_AsString(repr), |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1056 PyString_Size(repr) - 1); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1057 Py_DECREF(repr); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1058 if (temp == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1059 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1060 repr = temp; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1061 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1062 /* Append ", tzinfo=". */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1063 PyString_ConcatAndDel(&repr, PyString_FromString(", tzinfo=")); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1064 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1065 /* Append repr(tzinfo). */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1066 PyString_ConcatAndDel(&repr, PyObject_Repr(tzinfo)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1067 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1068 /* Add a closing paren. */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1069 PyString_ConcatAndDel(&repr, PyString_FromString(")")); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1070 return repr; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1071 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1072 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1073 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1074 * String format helpers. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1075 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1076 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1077 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
1078 format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1079 { |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35839
diff
changeset
|
1080 static const char *DayNames[] = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1081 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1082 }; |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35839
diff
changeset
|
1083 static const char *MonthNames[] = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1084 "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1085 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1086 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1087 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1088 char buffer[128]; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1089 int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1090 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1091 PyOS_snprintf(buffer, sizeof(buffer), "%s %s %2d %02d:%02d:%02d %04d", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1092 DayNames[wday], MonthNames[GET_MONTH(date) - 1], |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1093 GET_DAY(date), hours, minutes, seconds, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1094 GET_YEAR(date)); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1095 return PyString_FromString(buffer); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1096 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1097 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1098 /* Add an hours & minutes UTC offset string to buf. buf has no more than |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1099 * buflen bytes remaining. The UTC offset is gotten by calling |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1100 * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1101 * *buf, and that's all. Else the returned value is checked for sanity (an |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1102 * integer in range), and if that's OK it's converted to an hours & minutes |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1103 * string of the form |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1104 * sign HH sep MM |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1105 * Returns 0 if everything is OK. If the return value from utcoffset() is |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1106 * bogus, an appropriate exception is set and -1 is returned. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1107 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1108 static int |
|
26728
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1109 format_utcoffset(char *buf, size_t buflen, const char *sep, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1110 PyObject *tzinfo, PyObject *tzinfoarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1111 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1112 int offset; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1113 int hours; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1114 int minutes; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1115 char sign; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1116 int none; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1117 |
|
47587
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1118 assert(buflen >= 1); |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1119 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1120 offset = call_utcoffset(tzinfo, tzinfoarg, &none); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1121 if (offset == -1 && PyErr_Occurred()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1122 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1123 if (none) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1124 *buf = '\0'; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1125 return 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1126 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1127 sign = '+'; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1128 if (offset < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1129 sign = '-'; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1130 offset = - offset; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1131 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1132 hours = divmod(offset, 60, &minutes); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1133 PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1134 return 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1135 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1136 |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1137 static PyObject * |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1138 make_freplacement(PyObject *object) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1139 { |
|
45799
8018f5065661
Use a buffer large enough to ensure we don't overrun, even if the value
Neal Norwitz <nnorwitz@gmail.com>
parents:
45767
diff
changeset
|
1140 char freplacement[64]; |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1141 if (PyTime_Check(object)) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1142 sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1143 else if (PyDateTime_Check(object)) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1144 sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1145 else |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1146 sprintf(freplacement, "%06d", 0); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1147 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1148 return PyString_FromStringAndSize(freplacement, strlen(freplacement)); |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1149 } |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1150 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1151 /* I sure don't want to reproduce the strftime code from the time module, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1152 * so this imports the module and calls it. All the hair is due to |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1153 * giving special meanings to the %z, %Z and %f format codes via a |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1154 * preprocessing step on the format string. |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1155 * tzinfoarg is the argument to pass to the object's tzinfo method, if |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1156 * needed. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1157 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1158 static PyObject * |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1159 wrap_strftime(PyObject *object, const char *format, size_t format_len, |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1160 PyObject *timetuple, PyObject *tzinfoarg) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1161 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1162 PyObject *result = NULL; /* guilty until proved innocent */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1163 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1164 PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1165 PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1166 PyObject *freplacement = NULL; /* py string, replacement for %f */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1167 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1168 const char *pin; /* pointer to next char in input format */ |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1169 char ch; /* next char in input format */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1170 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1171 PyObject *newfmt = NULL; /* py string, the output format */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1172 char *pnew; /* pointer to available byte in output format */ |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1173 size_t totalnew; /* number bytes total in output format buffer, |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1174 exclusive of trailing \0 */ |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1175 size_t usednew; /* number bytes used so far in output format buffer */ |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1176 |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1177 const char *ptoappend; /* ptr to string to append to output buffer */ |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1178 size_t ntoappend; /* # of bytes to append to output buffer */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1179 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1180 assert(object && format && timetuple); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1181 |
|
26746
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1182 /* Give up if the year is before 1900. |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1183 * Python strftime() plays games with the year, and different |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1184 * games depending on whether envar PYTHON2K is set. This makes |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1185 * years before 1900 a nightmare, even if the platform strftime |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1186 * supports them (and not all do). |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1187 * We could get a lot farther here by avoiding Python's strftime |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1188 * wrapper and calling the C strftime() directly, but that isn't |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1189 * an option in the Python implementation of this module. |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1190 */ |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1191 { |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1192 long year; |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1193 PyObject *pyyear = PySequence_GetItem(timetuple, 0); |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1194 if (pyyear == NULL) return NULL; |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1195 assert(PyInt_Check(pyyear)); |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1196 year = PyInt_AsLong(pyyear); |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1197 Py_DECREF(pyyear); |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1198 if (year < 1900) { |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1199 PyErr_Format(PyExc_ValueError, "year=%ld is before " |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1200 "1900; the datetime strftime() " |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1201 "methods require year >= 1900", |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1202 year); |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1203 return NULL; |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1204 } |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1205 } |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26745
diff
changeset
|
1206 |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1207 /* Scan the input format, looking for %z/%Z/%f escapes, building |
|
26728
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1208 * a new format. Since computing the replacements for those codes |
|
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1209 * is expensive, don't unless they're actually used. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1210 */ |
|
47587
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1211 if (format_len > INT_MAX - 1) { |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1212 PyErr_NoMemory(); |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1213 goto Done; |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1214 } |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47560
diff
changeset
|
1215 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1216 totalnew = format_len + 1; /* realistic if no %z/%Z/%f */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1217 newfmt = PyString_FromStringAndSize(NULL, totalnew); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1218 if (newfmt == NULL) goto Done; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1219 pnew = PyString_AsString(newfmt); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1220 usednew = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1221 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1222 pin = format; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1223 while ((ch = *pin++) != '\0') { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1224 if (ch != '%') { |
|
26728
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1225 ptoappend = pin - 1; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1226 ntoappend = 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1227 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1228 else if ((ch = *pin++) == '\0') { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1229 /* There's a lone trailing %; doesn't make sense. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1230 PyErr_SetString(PyExc_ValueError, "strftime format " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1231 "ends with raw %"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1232 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1233 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1234 /* A % has been seen and ch is the character after it. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1235 else if (ch == 'z') { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1236 if (zreplacement == NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1237 /* format utcoffset */ |
|
26728
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1238 char buf[100]; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1239 PyObject *tzinfo = get_tzinfo_member(object); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1240 zreplacement = PyString_FromString(""); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1241 if (zreplacement == NULL) goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1242 if (tzinfo != Py_None && tzinfo != NULL) { |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1243 assert(tzinfoarg != NULL); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1244 if (format_utcoffset(buf, |
|
26728
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1245 sizeof(buf), |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1246 "", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1247 tzinfo, |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1248 tzinfoarg) < 0) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1249 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1250 Py_DECREF(zreplacement); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1251 zreplacement = PyString_FromString(buf); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1252 if (zreplacement == NULL) goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1253 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1254 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1255 assert(zreplacement != NULL); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1256 ptoappend = PyString_AS_STRING(zreplacement); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1257 ntoappend = PyString_GET_SIZE(zreplacement); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1258 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1259 else if (ch == 'Z') { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1260 /* format tzname */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1261 if (Zreplacement == NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1262 PyObject *tzinfo = get_tzinfo_member(object); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1263 Zreplacement = PyString_FromString(""); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1264 if (Zreplacement == NULL) goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1265 if (tzinfo != Py_None && tzinfo != NULL) { |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1266 PyObject *temp; |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1267 assert(tzinfoarg != NULL); |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
1268 temp = call_tzname(tzinfo, tzinfoarg); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1269 if (temp == NULL) goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1270 if (temp != Py_None) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1271 assert(PyString_Check(temp)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1272 /* Since the tzname is getting |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1273 * stuffed into the format, we |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1274 * have to double any % signs |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1275 * so that strftime doesn't |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1276 * treat them as format codes. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1277 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1278 Py_DECREF(Zreplacement); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1279 Zreplacement = PyObject_CallMethod( |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1280 temp, "replace", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1281 "ss", "%", "%%"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1282 Py_DECREF(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1283 if (Zreplacement == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1284 goto Done; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1285 if (!PyString_Check(Zreplacement)) { |
|
37088
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
36684
diff
changeset
|
1286 PyErr_SetString(PyExc_TypeError, "tzname.replace() did not return a string"); |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
36684
diff
changeset
|
1287 goto Done; |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
36684
diff
changeset
|
1288 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1289 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1290 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1291 Py_DECREF(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1292 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1293 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1294 assert(Zreplacement != NULL); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1295 ptoappend = PyString_AS_STRING(Zreplacement); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1296 ntoappend = PyString_GET_SIZE(Zreplacement); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1297 } |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1298 else if (ch == 'f') { |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1299 /* format microseconds */ |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1300 if (freplacement == NULL) { |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1301 freplacement = make_freplacement(object); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1302 if (freplacement == NULL) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1303 goto Done; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1304 } |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1305 assert(freplacement != NULL); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1306 assert(PyString_Check(freplacement)); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1307 ptoappend = PyString_AS_STRING(freplacement); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1308 ntoappend = PyString_GET_SIZE(freplacement); |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1309 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1310 else { |
|
26728
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1311 /* percent followed by neither z nor Z */ |
|
5fba51f3729b
format_utcoffset(): The natural type of the buflen arg is size_t, so
Tim Peters <tim.peters@gmail.com>
parents:
26712
diff
changeset
|
1312 ptoappend = pin - 2; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1313 ntoappend = 2; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1314 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1315 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1316 /* Append the ntoappend chars starting at ptoappend to |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1317 * the new format. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1318 */ |
|
37088
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
36684
diff
changeset
|
1319 assert(ptoappend != NULL); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1320 assert(ntoappend >= 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1321 if (ntoappend == 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1322 continue; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1323 while (usednew + ntoappend > totalnew) { |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1324 size_t bigger = totalnew << 1; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1325 if ((bigger >> 1) != totalnew) { /* overflow */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1326 PyErr_NoMemory(); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1327 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1328 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1329 if (_PyString_Resize(&newfmt, bigger) < 0) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1330 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1331 totalnew = bigger; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1332 pnew = PyString_AsString(newfmt) + usednew; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1333 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1334 memcpy(pnew, ptoappend, ntoappend); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1335 pnew += ntoappend; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1336 usednew += ntoappend; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1337 assert(usednew <= totalnew); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1338 } /* end while() */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1339 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
1340 if (_PyString_Resize(&newfmt, usednew) < 0) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1341 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1342 { |
|
44439
0cbe94bea2c7
Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents:
44350
diff
changeset
|
1343 PyObject *time = PyImport_ImportModuleNoBlock("time"); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1344 if (time == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1345 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1346 result = PyObject_CallMethod(time, "strftime", "OO", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1347 newfmt, timetuple); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1348 Py_DECREF(time); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1349 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1350 Done: |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
1351 Py_XDECREF(freplacement); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1352 Py_XDECREF(zreplacement); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1353 Py_XDECREF(Zreplacement); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1354 Py_XDECREF(newfmt); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1355 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1356 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1357 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1358 static char * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1359 isoformat_date(PyDateTime_Date *dt, char buffer[], int bufflen) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1360 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1361 int x; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1362 x = PyOS_snprintf(buffer, bufflen, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1363 "%04d-%02d-%02d", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1364 GET_YEAR(dt), GET_MONTH(dt), GET_DAY(dt)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1365 return buffer + x; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1366 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1367 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1368 static void |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1369 isoformat_time(PyDateTime_DateTime *dt, char buffer[], int bufflen) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1370 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1371 int us = DATE_GET_MICROSECOND(dt); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1372 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1373 PyOS_snprintf(buffer, bufflen, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1374 "%02d:%02d:%02d", /* 8 characters */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1375 DATE_GET_HOUR(dt), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1376 DATE_GET_MINUTE(dt), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1377 DATE_GET_SECOND(dt)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1378 if (us) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1379 PyOS_snprintf(buffer + 8, bufflen - 8, ".%06d", us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1380 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1381 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1382 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1383 * Wrap functions from the time module. These aren't directly available |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1384 * from C. Perhaps they should be. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1385 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1386 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1387 /* Call time.time() and return its result (a Python float). */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1388 static PyObject * |
|
26675
bc9056d847ef
Fix GCC warnings. It turns out two out of three pointed to real bugs!
Guido van Rossum <guido@python.org>
parents:
26673
diff
changeset
|
1389 time_time(void) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1390 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1391 PyObject *result = NULL; |
|
44439
0cbe94bea2c7
Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents:
44350
diff
changeset
|
1392 PyObject *time = PyImport_ImportModuleNoBlock("time"); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1393 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1394 if (time != NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1395 result = PyObject_CallMethod(time, "time", "()"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1396 Py_DECREF(time); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1397 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1398 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1399 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1400 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1401 /* Build a time.struct_time. The weekday and day number are automatically |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1402 * computed from the y,m,d args. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1403 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1404 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1405 build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1406 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1407 PyObject *time; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1408 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1409 |
|
44439
0cbe94bea2c7
Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents:
44350
diff
changeset
|
1410 time = PyImport_ImportModuleNoBlock("time"); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1411 if (time != NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1412 result = PyObject_CallMethod(time, "struct_time", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1413 "((iiiiiiiii))", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1414 y, m, d, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1415 hh, mm, ss, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1416 weekday(y, m, d), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1417 days_before_month(y, m) + d, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1418 dstflag); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1419 Py_DECREF(time); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1420 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1421 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1422 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1423 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1424 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1425 * Miscellaneous helpers. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1426 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1427 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1428 /* For obscure reasons, we need to use tp_richcompare instead of tp_compare. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1429 * The comparisons here all most naturally compute a cmp()-like result. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1430 * This little helper turns that into a bool result for rich comparisons. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1431 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1432 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1433 diff_to_bool(int diff, int op) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1434 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1435 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1436 int istrue; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1437 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1438 switch (op) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1439 case Py_EQ: istrue = diff == 0; break; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1440 case Py_NE: istrue = diff != 0; break; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1441 case Py_LE: istrue = diff <= 0; break; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1442 case Py_GE: istrue = diff >= 0; break; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1443 case Py_LT: istrue = diff < 0; break; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1444 case Py_GT: istrue = diff > 0; break; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1445 default: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1446 assert(! "op unknown"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1447 istrue = 0; /* To shut up compiler */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1448 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1449 result = istrue ? Py_True : Py_False; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1450 Py_INCREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1451 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1452 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1453 |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1454 /* Raises a "can't compare" TypeError and returns NULL. */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1455 static PyObject * |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1456 cmperror(PyObject *a, PyObject *b) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1457 { |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1458 PyErr_Format(PyExc_TypeError, |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1459 "can't compare %s to %s", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
1460 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1461 return NULL; |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1462 } |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1463 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1464 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1465 * Cached Python objects; these are set by the module init function. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1466 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1467 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1468 /* Conversion factors. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1469 static PyObject *us_per_us = NULL; /* 1 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1470 static PyObject *us_per_ms = NULL; /* 1000 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1471 static PyObject *us_per_second = NULL; /* 1000000 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1472 static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1473 static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python long */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1474 static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python long */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1475 static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1476 static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1477 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1478 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1479 * Class implementations. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1480 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1481 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1482 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1483 * PyDateTime_Delta implementation. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1484 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1485 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1486 /* Convert a timedelta to a number of us, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1487 * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1488 * as a Python int or long. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1489 * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1490 * due to ubiquitous overflow possibilities. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1491 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1492 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1493 delta_to_microseconds(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1494 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1495 PyObject *x1 = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1496 PyObject *x2 = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1497 PyObject *x3 = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1498 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1499 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1500 x1 = PyInt_FromLong(GET_TD_DAYS(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1501 if (x1 == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1502 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1503 x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1504 if (x2 == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1505 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1506 Py_DECREF(x1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1507 x1 = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1508 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1509 /* x2 has days in seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1510 x1 = PyInt_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1511 if (x1 == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1512 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1513 x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1514 if (x3 == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1515 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1516 Py_DECREF(x1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1517 Py_DECREF(x2); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1518 x1 = x2 = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1519 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1520 /* x3 has days+seconds in seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1521 x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1522 if (x1 == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1523 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1524 Py_DECREF(x3); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1525 x3 = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1526 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1527 /* x1 has days+seconds in us */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1528 x2 = PyInt_FromLong(GET_TD_MICROSECONDS(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1529 if (x2 == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1530 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1531 result = PyNumber_Add(x1, x2); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1532 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1533 Done: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1534 Py_XDECREF(x1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1535 Py_XDECREF(x2); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1536 Py_XDECREF(x3); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1537 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1538 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1539 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1540 /* Convert a number of us (as a Python int or long) to a timedelta. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1541 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1542 static PyObject * |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
1543 microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1544 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1545 int us; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1546 int s; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1547 int d; |
| 26711 | 1548 long temp; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1549 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1550 PyObject *tuple = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1551 PyObject *num = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1552 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1553 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1554 tuple = PyNumber_Divmod(pyus, us_per_second); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1555 if (tuple == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1556 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1557 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1558 num = PyTuple_GetItem(tuple, 1); /* us */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1559 if (num == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1560 goto Done; |
| 26711 | 1561 temp = PyLong_AsLong(num); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1562 num = NULL; |
| 26711 | 1563 if (temp == -1 && PyErr_Occurred()) |
| 1564 goto Done; | |
| 1565 assert(0 <= temp && temp < 1000000); | |
| 1566 us = (int)temp; | |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1567 if (us < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1568 /* The divisor was positive, so this must be an error. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1569 assert(PyErr_Occurred()); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1570 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1571 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1572 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1573 num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1574 if (num == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1575 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1576 Py_INCREF(num); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1577 Py_DECREF(tuple); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1578 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1579 tuple = PyNumber_Divmod(num, seconds_per_day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1580 if (tuple == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1581 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1582 Py_DECREF(num); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1583 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1584 num = PyTuple_GetItem(tuple, 1); /* seconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1585 if (num == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1586 goto Done; |
| 26711 | 1587 temp = PyLong_AsLong(num); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1588 num = NULL; |
| 26711 | 1589 if (temp == -1 && PyErr_Occurred()) |
| 1590 goto Done; | |
| 1591 assert(0 <= temp && temp < 24*3600); | |
| 1592 s = (int)temp; | |
| 1593 | |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1594 if (s < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1595 /* The divisor was positive, so this must be an error. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1596 assert(PyErr_Occurred()); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1597 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1598 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1599 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1600 num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1601 if (num == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1602 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1603 Py_INCREF(num); |
| 26711 | 1604 temp = PyLong_AsLong(num); |
| 1605 if (temp == -1 && PyErr_Occurred()) | |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1606 goto Done; |
| 26711 | 1607 d = (int)temp; |
| 1608 if ((long)d != temp) { | |
| 1609 PyErr_SetString(PyExc_OverflowError, "normalized days too " | |
| 1610 "large to fit in a C int"); | |
| 1611 goto Done; | |
| 1612 } | |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
1613 result = new_delta_ex(d, s, us, 0, type); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1614 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1615 Done: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1616 Py_XDECREF(tuple); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1617 Py_XDECREF(num); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1618 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1619 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1620 |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
1621 #define microseconds_to_delta(pymicros) \ |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
1622 microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
1623 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1624 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1625 multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1626 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1627 PyObject *pyus_in; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1628 PyObject *pyus_out; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1629 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1630 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1631 pyus_in = delta_to_microseconds(delta); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1632 if (pyus_in == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1633 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1634 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1635 pyus_out = PyNumber_Multiply(pyus_in, intobj); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1636 Py_DECREF(pyus_in); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1637 if (pyus_out == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1638 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1639 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1640 result = microseconds_to_delta(pyus_out); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1641 Py_DECREF(pyus_out); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1642 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1643 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1644 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1645 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1646 divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1647 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1648 PyObject *pyus_in; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1649 PyObject *pyus_out; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1650 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1651 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1652 pyus_in = delta_to_microseconds(delta); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1653 if (pyus_in == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1654 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1655 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1656 pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1657 Py_DECREF(pyus_in); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1658 if (pyus_out == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1659 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1660 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1661 result = microseconds_to_delta(pyus_out); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1662 Py_DECREF(pyus_out); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1663 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1664 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1665 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1666 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1667 delta_add(PyObject *left, PyObject *right) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1668 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1669 PyObject *result = Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1670 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1671 if (PyDelta_Check(left) && PyDelta_Check(right)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1672 /* delta + delta */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1673 /* The C-level additions can't overflow because of the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1674 * invariant bounds. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1675 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1676 int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1677 int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1678 int microseconds = GET_TD_MICROSECONDS(left) + |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1679 GET_TD_MICROSECONDS(right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1680 result = new_delta(days, seconds, microseconds, 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1681 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1682 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1683 if (result == Py_NotImplemented) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1684 Py_INCREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1685 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1686 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1687 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1688 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1689 delta_negative(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1690 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1691 return new_delta(-GET_TD_DAYS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1692 -GET_TD_SECONDS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1693 -GET_TD_MICROSECONDS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1694 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1695 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1696 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1697 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1698 delta_positive(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1699 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1700 /* Could optimize this (by returning self) if this isn't a |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1701 * subclass -- but who uses unary + ? Approximately nobody. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1702 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1703 return new_delta(GET_TD_DAYS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1704 GET_TD_SECONDS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1705 GET_TD_MICROSECONDS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1706 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1707 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1708 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1709 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1710 delta_abs(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1711 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1712 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1713 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1714 assert(GET_TD_MICROSECONDS(self) >= 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1715 assert(GET_TD_SECONDS(self) >= 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1716 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1717 if (GET_TD_DAYS(self) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1718 result = delta_negative(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1719 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1720 result = delta_positive(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1721 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1722 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1723 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1724 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1725 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1726 delta_subtract(PyObject *left, PyObject *right) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1727 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1728 PyObject *result = Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1729 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1730 if (PyDelta_Check(left) && PyDelta_Check(right)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1731 /* delta - delta */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1732 PyObject *minus_right = PyNumber_Negative(right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1733 if (minus_right) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1734 result = delta_add(left, minus_right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1735 Py_DECREF(minus_right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1736 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1737 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1738 result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1739 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1740 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1741 if (result == Py_NotImplemented) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1742 Py_INCREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1743 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1744 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1745 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1746 /* This is more natural as a tp_compare, but doesn't work then: for whatever |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1747 * reason, Python's try_3way_compare ignores tp_compare unless |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1748 * PyInstance_Check returns true, but these aren't old-style classes. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1749 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1750 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1751 delta_richcompare(PyDateTime_Delta *self, PyObject *other, int op) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1752 { |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1753 int diff = 42; /* nonsense */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1754 |
|
27765
7b5043c0ff47
timedelta comparison and datetime addition: as the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
1755 if (PyDelta_Check(other)) { |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1756 diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1757 if (diff == 0) { |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1758 diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1759 if (diff == 0) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1760 diff = GET_TD_MICROSECONDS(self) - |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1761 GET_TD_MICROSECONDS(other); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1762 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1763 } |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1764 else if (op == Py_EQ || op == Py_NE) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1765 diff = 1; /* any non-zero value will do */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1766 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1767 else /* stop this from falling back to address comparison */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1768 return cmperror((PyObject *)self, other); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
1769 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1770 return diff_to_bool(diff, op); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1771 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1772 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1773 static PyObject *delta_getstate(PyDateTime_Delta *self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1774 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1775 static long |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1776 delta_hash(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1777 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1778 if (self->hashcode == -1) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1779 PyObject *temp = delta_getstate(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1780 if (temp != NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1781 self->hashcode = PyObject_Hash(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1782 Py_DECREF(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1783 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1784 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1785 return self->hashcode; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1786 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1787 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1788 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1789 delta_multiply(PyObject *left, PyObject *right) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1790 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1791 PyObject *result = Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1792 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1793 if (PyDelta_Check(left)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1794 /* delta * ??? */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1795 if (PyInt_Check(right) || PyLong_Check(right)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1796 result = multiply_int_timedelta(right, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1797 (PyDateTime_Delta *) left); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1798 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1799 else if (PyInt_Check(left) || PyLong_Check(left)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1800 result = multiply_int_timedelta(left, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1801 (PyDateTime_Delta *) right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1802 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1803 if (result == Py_NotImplemented) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1804 Py_INCREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1805 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1806 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1807 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1808 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1809 delta_divide(PyObject *left, PyObject *right) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1810 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1811 PyObject *result = Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1812 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1813 if (PyDelta_Check(left)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1814 /* delta * ??? */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1815 if (PyInt_Check(right) || PyLong_Check(right)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1816 result = divide_timedelta_int( |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1817 (PyDateTime_Delta *)left, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1818 right); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1819 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1820 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1821 if (result == Py_NotImplemented) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1822 Py_INCREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1823 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1824 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1825 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1826 /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1827 * timedelta constructor. sofar is the # of microseconds accounted for |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1828 * so far, and there are factor microseconds per current unit, the number |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1829 * of which is given by num. num * factor is added to sofar in a |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1830 * numerically careful way, and that's the result. Any fractional |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1831 * microseconds left over (this can happen if num is a float type) are |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1832 * added into *leftover. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1833 * Note that there are many ways this can give an error (NULL) return. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1834 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1835 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1836 accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1837 double *leftover) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1838 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1839 PyObject *prod; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1840 PyObject *sum; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1841 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1842 assert(num != NULL); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1843 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1844 if (PyInt_Check(num) || PyLong_Check(num)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1845 prod = PyNumber_Multiply(num, factor); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1846 if (prod == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1847 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1848 sum = PyNumber_Add(sofar, prod); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1849 Py_DECREF(prod); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1850 return sum; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1851 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1852 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1853 if (PyFloat_Check(num)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1854 double dnum; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1855 double fracpart; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1856 double intpart; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1857 PyObject *x; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1858 PyObject *y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1859 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1860 /* The Plan: decompose num into an integer part and a |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1861 * fractional part, num = intpart + fracpart. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1862 * Then num * factor == |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1863 * intpart * factor + fracpart * factor |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1864 * and the LHS can be computed exactly in long arithmetic. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1865 * The RHS is again broken into an int part and frac part. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1866 * and the frac part is added into *leftover. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1867 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1868 dnum = PyFloat_AsDouble(num); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1869 if (dnum == -1.0 && PyErr_Occurred()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1870 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1871 fracpart = modf(dnum, &intpart); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1872 x = PyLong_FromDouble(intpart); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1873 if (x == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1874 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1875 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1876 prod = PyNumber_Multiply(x, factor); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1877 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1878 if (prod == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1879 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1880 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1881 sum = PyNumber_Add(sofar, prod); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1882 Py_DECREF(prod); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1883 if (sum == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1884 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1885 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1886 if (fracpart == 0.0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1887 return sum; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1888 /* So far we've lost no information. Dealing with the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1889 * fractional part requires float arithmetic, and may |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1890 * lose a little info. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1891 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1892 assert(PyInt_Check(factor) || PyLong_Check(factor)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1893 if (PyInt_Check(factor)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1894 dnum = (double)PyInt_AsLong(factor); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1895 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1896 dnum = PyLong_AsDouble(factor); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1897 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1898 dnum *= fracpart; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1899 fracpart = modf(dnum, &intpart); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1900 x = PyLong_FromDouble(intpart); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1901 if (x == NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1902 Py_DECREF(sum); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1903 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1904 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1905 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1906 y = PyNumber_Add(sum, x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1907 Py_DECREF(sum); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1908 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1909 *leftover += fracpart; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1910 return y; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1911 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1912 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1913 PyErr_Format(PyExc_TypeError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1914 "unsupported type for timedelta %s component: %s", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
1915 tag, Py_TYPE(num)->tp_name); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1916 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1917 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1918 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1919 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1920 delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1921 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1922 PyObject *self = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1923 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1924 /* Argument objects. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1925 PyObject *day = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1926 PyObject *second = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1927 PyObject *us = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1928 PyObject *ms = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1929 PyObject *minute = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1930 PyObject *hour = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1931 PyObject *week = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1932 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1933 PyObject *x = NULL; /* running sum of microseconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1934 PyObject *y = NULL; /* temp sum of microseconds */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1935 double leftover_us = 0.0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1936 |
| 36684 | 1937 static char *keywords[] = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1938 "days", "seconds", "microseconds", "milliseconds", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1939 "minutes", "hours", "weeks", NULL |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1940 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1941 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1942 if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1943 keywords, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1944 &day, &second, &us, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1945 &ms, &minute, &hour, &week) == 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1946 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1947 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1948 x = PyInt_FromLong(0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1949 if (x == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1950 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1951 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1952 #define CLEANUP \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1953 Py_DECREF(x); \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1954 x = y; \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1955 if (x == NULL) \ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1956 goto Done |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1957 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1958 if (us) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1959 y = accum("microseconds", x, us, us_per_us, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1960 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1961 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1962 if (ms) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1963 y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1964 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1965 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1966 if (second) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1967 y = accum("seconds", x, second, us_per_second, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1968 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1969 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1970 if (minute) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1971 y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1972 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1973 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1974 if (hour) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1975 y = accum("hours", x, hour, us_per_hour, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1976 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1977 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1978 if (day) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1979 y = accum("days", x, day, us_per_day, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1980 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1981 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1982 if (week) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1983 y = accum("weeks", x, week, us_per_week, &leftover_us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1984 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1985 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1986 if (leftover_us) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1987 /* Round to nearest whole # of us, and add into x. */ |
|
27029
d279d763f462
SF bug 661086: datetime.today() truncates microseconds.
Tim Peters <tim.peters@gmail.com>
parents:
27011
diff
changeset
|
1988 PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1989 if (temp == NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1990 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1991 goto Done; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1992 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1993 y = PyNumber_Add(x, temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1994 Py_DECREF(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1995 CLEANUP; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1996 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1997 |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
1998 self = microseconds_to_delta_ex(x, type); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1999 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2000 Done: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2001 return self; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2002 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2003 #undef CLEANUP |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2004 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2005 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2006 static int |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2007 delta_nonzero(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2008 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2009 return (GET_TD_DAYS(self) != 0 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2010 || GET_TD_SECONDS(self) != 0 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2011 || GET_TD_MICROSECONDS(self) != 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2012 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2013 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2014 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2015 delta_repr(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2016 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2017 if (GET_TD_MICROSECONDS(self) != 0) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2018 return PyString_FromFormat("%s(%d, %d, %d)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2019 Py_TYPE(self)->tp_name, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2020 GET_TD_DAYS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2021 GET_TD_SECONDS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2022 GET_TD_MICROSECONDS(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2023 if (GET_TD_SECONDS(self) != 0) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2024 return PyString_FromFormat("%s(%d, %d)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2025 Py_TYPE(self)->tp_name, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2026 GET_TD_DAYS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2027 GET_TD_SECONDS(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2028 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2029 return PyString_FromFormat("%s(%d)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2030 Py_TYPE(self)->tp_name, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2031 GET_TD_DAYS(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2032 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2033 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2034 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2035 delta_str(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2036 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2037 int days = GET_TD_DAYS(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2038 int seconds = GET_TD_SECONDS(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2039 int us = GET_TD_MICROSECONDS(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2040 int hours; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2041 int minutes; |
|
26706
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2042 char buf[100]; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2043 char *pbuf = buf; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2044 size_t buflen = sizeof(buf); |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2045 int n; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2046 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2047 minutes = divmod(seconds, 60, &seconds); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2048 hours = divmod(minutes, 60, &minutes); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2049 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2050 if (days) { |
|
26706
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2051 n = PyOS_snprintf(pbuf, buflen, "%d day%s, ", days, |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2052 (days == 1 || days == -1) ? "" : "s"); |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2053 if (n < 0 || (size_t)n >= buflen) |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2054 goto Fail; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2055 pbuf += n; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2056 buflen -= (size_t)n; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2057 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2058 |
|
26706
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2059 n = PyOS_snprintf(pbuf, buflen, "%d:%02d:%02d", |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2060 hours, minutes, seconds); |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2061 if (n < 0 || (size_t)n >= buflen) |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2062 goto Fail; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2063 pbuf += n; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2064 buflen -= (size_t)n; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2065 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2066 if (us) { |
|
26706
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2067 n = PyOS_snprintf(pbuf, buflen, ".%06d", us); |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2068 if (n < 0 || (size_t)n >= buflen) |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2069 goto Fail; |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2070 pbuf += n; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2071 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2072 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2073 return PyString_FromStringAndSize(buf, pbuf - buf); |
|
26706
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2074 |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2075 Fail: |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2076 PyErr_SetString(PyExc_SystemError, "goofy result from PyOS_snprintf"); |
|
d64214694083
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
Tim Peters <tim.peters@gmail.com>
parents:
26692
diff
changeset
|
2077 return NULL; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2078 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2079 |
|
27572
f235f135f3f8
All over: changed comments to reflect pickling is straightforward now,
Tim Peters <tim.peters@gmail.com>
parents:
27571
diff
changeset
|
2080 /* Pickle support, a simple use of __reduce__. */ |
|
f235f135f3f8
All over: changed comments to reflect pickling is straightforward now,
Tim Peters <tim.peters@gmail.com>
parents:
27571
diff
changeset
|
2081 |
|
27575
3b9fb57fac34
There's no good reason for datetime objects to expose __getstate__()
Tim Peters <tim.peters@gmail.com>
parents:
27573
diff
changeset
|
2082 /* __getstate__ isn't exposed */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2083 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2084 delta_getstate(PyDateTime_Delta *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2085 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2086 return Py_BuildValue("iii", GET_TD_DAYS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2087 GET_TD_SECONDS(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2088 GET_TD_MICROSECONDS(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2089 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2090 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2091 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2092 delta_reduce(PyDateTime_Delta* self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2093 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2094 return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2095 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2096 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2097 #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2098 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2099 static PyMemberDef delta_members[] = { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2100 |
|
26712
04b3a9f6be41
Fix another long vs int mismatch. test_datetime now passes on alpha
Neal Norwitz <nnorwitz@gmail.com>
parents:
26711
diff
changeset
|
2101 {"days", T_INT, OFFSET(days), READONLY, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2102 PyDoc_STR("Number of days.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2103 |
|
26712
04b3a9f6be41
Fix another long vs int mismatch. test_datetime now passes on alpha
Neal Norwitz <nnorwitz@gmail.com>
parents:
26711
diff
changeset
|
2104 {"seconds", T_INT, OFFSET(seconds), READONLY, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2105 PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2106 |
|
26712
04b3a9f6be41
Fix another long vs int mismatch. test_datetime now passes on alpha
Neal Norwitz <nnorwitz@gmail.com>
parents:
26711
diff
changeset
|
2107 {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2108 PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2109 {NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2110 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2111 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2112 static PyMethodDef delta_methods[] = { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2113 {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2114 PyDoc_STR("__reduce__() -> (cls, state)")}, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2115 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2116 {NULL, NULL}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2117 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2118 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2119 static char delta_doc[] = |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2120 PyDoc_STR("Difference between two datetime values."); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2121 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2122 static PyNumberMethods delta_as_number = { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2123 delta_add, /* nb_add */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2124 delta_subtract, /* nb_subtract */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2125 delta_multiply, /* nb_multiply */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2126 delta_divide, /* nb_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2127 0, /* nb_remainder */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2128 0, /* nb_divmod */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2129 0, /* nb_power */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2130 (unaryfunc)delta_negative, /* nb_negative */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2131 (unaryfunc)delta_positive, /* nb_positive */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2132 (unaryfunc)delta_abs, /* nb_absolute */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2133 (inquiry)delta_nonzero, /* nb_nonzero */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2134 0, /*nb_invert*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2135 0, /*nb_lshift*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2136 0, /*nb_rshift*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2137 0, /*nb_and*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2138 0, /*nb_xor*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2139 0, /*nb_or*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2140 0, /*nb_coerce*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2141 0, /*nb_int*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2142 0, /*nb_long*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2143 0, /*nb_float*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2144 0, /*nb_oct*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2145 0, /*nb_hex*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2146 0, /*nb_inplace_add*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2147 0, /*nb_inplace_subtract*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2148 0, /*nb_inplace_multiply*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2149 0, /*nb_inplace_divide*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2150 0, /*nb_inplace_remainder*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2151 0, /*nb_inplace_power*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2152 0, /*nb_inplace_lshift*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2153 0, /*nb_inplace_rshift*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2154 0, /*nb_inplace_and*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2155 0, /*nb_inplace_xor*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2156 0, /*nb_inplace_or*/ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2157 delta_divide, /* nb_floor_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2158 0, /* nb_true_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2159 0, /* nb_inplace_floor_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2160 0, /* nb_inplace_true_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2161 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2162 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2163 static PyTypeObject PyDateTime_DeltaType = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41816
diff
changeset
|
2164 PyVarObject_HEAD_INIT(NULL, 0) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2165 "datetime.timedelta", /* tp_name */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2166 sizeof(PyDateTime_Delta), /* tp_basicsize */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2167 0, /* tp_itemsize */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2168 0, /* tp_dealloc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2169 0, /* tp_print */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2170 0, /* tp_getattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2171 0, /* tp_setattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2172 0, /* tp_compare */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2173 (reprfunc)delta_repr, /* tp_repr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2174 &delta_as_number, /* tp_as_number */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2175 0, /* tp_as_sequence */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2176 0, /* tp_as_mapping */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2177 (hashfunc)delta_hash, /* tp_hash */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2178 0, /* tp_call */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2179 (reprfunc)delta_str, /* tp_str */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2180 PyObject_GenericGetAttr, /* tp_getattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2181 0, /* tp_setattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2182 0, /* tp_as_buffer */ |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
2183 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
2184 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2185 delta_doc, /* tp_doc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2186 0, /* tp_traverse */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2187 0, /* tp_clear */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2188 (richcmpfunc)delta_richcompare, /* tp_richcompare */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2189 0, /* tp_weaklistoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2190 0, /* tp_iter */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2191 0, /* tp_iternext */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2192 delta_methods, /* tp_methods */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2193 delta_members, /* tp_members */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2194 0, /* tp_getset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2195 0, /* tp_base */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2196 0, /* tp_dict */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2197 0, /* tp_descr_get */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2198 0, /* tp_descr_set */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2199 0, /* tp_dictoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2200 0, /* tp_init */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2201 0, /* tp_alloc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2202 delta_new, /* tp_new */ |
|
29138
c310f9ddfdcb
Turns out there wasn't a need to define tp_free for any of the types here.
Tim Peters <tim.peters@gmail.com>
parents:
29136
diff
changeset
|
2203 0, /* tp_free */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2204 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2205 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2206 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2207 * PyDateTime_Date implementation. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2208 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2209 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2210 /* Accessor properties. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2211 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2212 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2213 date_year(PyDateTime_Date *self, void *unused) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2214 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2215 return PyInt_FromLong(GET_YEAR(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2216 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2217 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2218 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2219 date_month(PyDateTime_Date *self, void *unused) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2220 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2221 return PyInt_FromLong(GET_MONTH(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2222 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2223 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2224 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2225 date_day(PyDateTime_Date *self, void *unused) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2226 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2227 return PyInt_FromLong(GET_DAY(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2228 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2229 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2230 static PyGetSetDef date_getset[] = { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2231 {"year", (getter)date_year}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2232 {"month", (getter)date_month}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2233 {"day", (getter)date_day}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2234 {NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2235 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2236 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2237 /* Constructors. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2238 |
| 36684 | 2239 static char *date_kws[] = {"year", "month", "day", NULL}; |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2240 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2241 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2242 date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2243 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2244 PyObject *self = NULL; |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2245 PyObject *state; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2246 int year; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2247 int month; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2248 int day; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2249 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2250 /* Check for invocation from pickle with __getstate__ state */ |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2251 if (PyTuple_GET_SIZE(args) == 1 && |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2252 PyString_Check(state = PyTuple_GET_ITEM(args, 0)) && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2253 PyString_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2254 MONTH_IS_SANE(PyString_AS_STRING(state)[2])) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2255 { |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2256 PyDateTime_Date *me; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2257 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
2258 me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2259 if (me != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2260 char *pdata = PyString_AS_STRING(state); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2261 memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2262 me->hashcode = -1; |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2263 } |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
2264 return (PyObject *)me; |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2265 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2266 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2267 if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2268 &year, &month, &day)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2269 if (check_date_args(year, month, day) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2270 return NULL; |
|
28609
6952373e8fe5
The date class is now properly subclassable. (SF bug #720908)
Guido van Rossum <guido@python.org>
parents:
27765
diff
changeset
|
2271 self = new_date_ex(year, month, day, type); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2272 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2273 return self; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2274 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2275 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2276 /* Return new date from localtime(t). */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2277 static PyObject * |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
2278 date_local_from_time_t(PyObject *cls, double ts) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2279 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2280 struct tm *tm; |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
2281 time_t t; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2282 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2283 |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
2284 t = _PyTime_DoubleToTimet(ts); |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
2285 if (t == (time_t)-1 && PyErr_Occurred()) |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
2286 return NULL; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2287 tm = localtime(&t); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2288 if (tm) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2289 result = PyObject_CallFunction(cls, "iii", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2290 tm->tm_year + 1900, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2291 tm->tm_mon + 1, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2292 tm->tm_mday); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2293 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2294 PyErr_SetString(PyExc_ValueError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2295 "timestamp out of range for " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2296 "platform localtime() function"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2297 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2298 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2299 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2300 /* Return new date from current time. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2301 * We say this is equivalent to fromtimestamp(time.time()), and the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2302 * only way to be sure of that is to *call* time.time(). That's not |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2303 * generally the same as calling C's time. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2304 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2305 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2306 date_today(PyObject *cls, PyObject *dummy) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2307 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2308 PyObject *time; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2309 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2310 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2311 time = time_time(); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2312 if (time == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2313 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2314 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2315 /* Note well: today() is a class method, so this may not call |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2316 * date.fromtimestamp. For example, it may call |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2317 * datetime.fromtimestamp. That's why we need all the accuracy |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2318 * time.time() delivers; if someone were gonzo about optimization, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2319 * date.today() could get away with plain C time(). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2320 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2321 result = PyObject_CallMethod(cls, "fromtimestamp", "O", time); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2322 Py_DECREF(time); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2323 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2324 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2325 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2326 /* Return new date from given timestamp (Python timestamp -- a double). */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2327 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2328 date_fromtimestamp(PyObject *cls, PyObject *args) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2329 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2330 double timestamp; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2331 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2332 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2333 if (PyArg_ParseTuple(args, "d:fromtimestamp", ×tamp)) |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
2334 result = date_local_from_time_t(cls, timestamp); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2335 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2336 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2337 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2338 /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2339 * the ordinal is out of range. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2340 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2341 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2342 date_fromordinal(PyObject *cls, PyObject *args) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2343 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2344 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2345 int ordinal; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2346 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2347 if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2348 int year; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2349 int month; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2350 int day; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2351 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2352 if (ordinal < 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2353 PyErr_SetString(PyExc_ValueError, "ordinal must be " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2354 ">= 1"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2355 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2356 ord_to_ymd(ordinal, &year, &month, &day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2357 result = PyObject_CallFunction(cls, "iii", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2358 year, month, day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2359 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2360 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2361 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2362 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2363 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2364 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2365 * Date arithmetic. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2366 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2367 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2368 /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2369 * instead. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2370 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2371 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2372 add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2373 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2374 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2375 int year = GET_YEAR(date); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2376 int month = GET_MONTH(date); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2377 int deltadays = GET_TD_DAYS(delta); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2378 /* C-level overflow is impossible because |deltadays| < 1e9. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2379 int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2380 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2381 if (normalize_date(&year, &month, &day) >= 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2382 result = new_date(year, month, day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2383 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2384 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2385 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2386 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2387 date_add(PyObject *left, PyObject *right) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2388 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2389 if (PyDateTime_Check(left) || PyDateTime_Check(right)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2390 Py_INCREF(Py_NotImplemented); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2391 return Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2392 } |
|
27765
7b5043c0ff47
timedelta comparison and datetime addition: as the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2393 if (PyDate_Check(left)) { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2394 /* date + ??? */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2395 if (PyDelta_Check(right)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2396 /* date + delta */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2397 return add_date_timedelta((PyDateTime_Date *) left, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2398 (PyDateTime_Delta *) right, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2399 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2400 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2401 else { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2402 /* ??? + date |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2403 * 'right' must be one of us, or we wouldn't have been called |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2404 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2405 if (PyDelta_Check(left)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2406 /* delta + date */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2407 return add_date_timedelta((PyDateTime_Date *) right, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2408 (PyDateTime_Delta *) left, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2409 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2410 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2411 Py_INCREF(Py_NotImplemented); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2412 return Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2413 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2414 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2415 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2416 date_subtract(PyObject *left, PyObject *right) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2417 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2418 if (PyDateTime_Check(left) || PyDateTime_Check(right)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2419 Py_INCREF(Py_NotImplemented); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2420 return Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2421 } |
|
27765
7b5043c0ff47
timedelta comparison and datetime addition: as the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2422 if (PyDate_Check(left)) { |
|
7b5043c0ff47
timedelta comparison and datetime addition: as the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2423 if (PyDate_Check(right)) { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2424 /* date - date */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2425 int left_ord = ymd_to_ord(GET_YEAR(left), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2426 GET_MONTH(left), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2427 GET_DAY(left)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2428 int right_ord = ymd_to_ord(GET_YEAR(right), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2429 GET_MONTH(right), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2430 GET_DAY(right)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2431 return new_delta(left_ord - right_ord, 0, 0, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2432 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2433 if (PyDelta_Check(right)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2434 /* date - delta */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2435 return add_date_timedelta((PyDateTime_Date *) left, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2436 (PyDateTime_Delta *) right, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2437 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2438 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2439 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2440 Py_INCREF(Py_NotImplemented); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2441 return Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2442 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2443 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2444 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2445 /* Various ways to turn a date into a string. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2446 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2447 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2448 date_repr(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2449 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2450 char buffer[1028]; |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
2451 const char *type_name; |
|
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
2452 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2453 type_name = Py_TYPE(self)->tp_name; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2454 PyOS_snprintf(buffer, sizeof(buffer), "%s(%d, %d, %d)", |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
2455 type_name, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2456 GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2457 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2458 return PyString_FromString(buffer); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2459 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2460 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2461 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2462 date_isoformat(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2463 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2464 char buffer[128]; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2465 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2466 isoformat_date(self, buffer, sizeof(buffer)); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2467 return PyString_FromString(buffer); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2468 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2469 |
|
28928
c798d28dee46
SF patch 731504: Typo in datetimemodule.c comment.
Tim Peters <tim.peters@gmail.com>
parents:
28609
diff
changeset
|
2470 /* str() calls the appropriate isoformat() method. */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2471 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2472 date_str(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2473 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2474 return PyObject_CallMethod((PyObject *)self, "isoformat", "()"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2475 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2476 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2477 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2478 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2479 date_ctime(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2480 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2481 return format_ctime(self, 0, 0, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2482 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2483 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2484 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2485 date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2486 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2487 /* This method can be inherited, and needs to call the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2488 * timetuple() method appropriate to self's class. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2489 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2490 PyObject *result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2491 PyObject *tuple; |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2492 const char *format; |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2493 Py_ssize_t format_len; |
| 36684 | 2494 static char *keywords[] = {"format", NULL}; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2495 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2496 if (! PyArg_ParseTupleAndKeywords(args, kw, "s#:strftime", keywords, |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2497 &format, &format_len)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2498 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2499 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2500 tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2501 if (tuple == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2502 return NULL; |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2503 result = wrap_strftime((PyObject *)self, format, format_len, tuple, |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
2504 (PyObject *)self); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2505 Py_DECREF(tuple); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2506 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2507 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2508 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2509 static PyObject * |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2510 date_format(PyDateTime_Date *self, PyObject *args) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2511 { |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2512 PyObject *format; |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2513 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2514 if (!PyArg_ParseTuple(args, "O:__format__", &format)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2515 return NULL; |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2516 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2517 /* Check for str or unicode */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2518 if (PyString_Check(format)) { |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2519 /* If format is zero length, return str(self) */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2520 if (PyString_GET_SIZE(format) == 0) |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2521 return PyObject_Str((PyObject *)self); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2522 } else if (PyUnicode_Check(format)) { |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2523 /* If format is zero length, return str(self) */ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2524 if (PyUnicode_GET_SIZE(format) == 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2525 return PyObject_Unicode((PyObject *)self); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2526 } else { |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2527 PyErr_Format(PyExc_ValueError, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2528 "__format__ expects str or unicode, not %.200s", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2529 Py_TYPE(format)->tp_name); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2530 return NULL; |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2531 } |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2532 return PyObject_CallMethod((PyObject *)self, "strftime", "O", format); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2533 } |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2534 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2535 /* ISO methods. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2536 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2537 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2538 date_isoweekday(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2539 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2540 int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2541 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2542 return PyInt_FromLong(dow + 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2543 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2544 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2545 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2546 date_isocalendar(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2547 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2548 int year = GET_YEAR(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2549 int week1_monday = iso_week1_monday(year); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2550 int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2551 int week; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2552 int day; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2553 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2554 week = divmod(today - week1_monday, 7, &day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2555 if (week < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2556 --year; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2557 week1_monday = iso_week1_monday(year); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2558 week = divmod(today - week1_monday, 7, &day); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2559 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2560 else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2561 ++year; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2562 week = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2563 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2564 return Py_BuildValue("iii", year, week + 1, day + 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2565 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2566 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2567 /* Miscellaneous methods. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2568 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2569 /* This is more natural as a tp_compare, but doesn't work then: for whatever |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2570 * reason, Python's try_3way_compare ignores tp_compare unless |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2571 * PyInstance_Check returns true, but these aren't old-style classes. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2572 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2573 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2574 date_richcompare(PyDateTime_Date *self, PyObject *other, int op) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2575 { |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2576 int diff = 42; /* nonsense */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2577 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2578 if (PyDate_Check(other)) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2579 diff = memcmp(self->data, ((PyDateTime_Date *)other)->data, |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2580 _PyDateTime_DATE_DATASIZE); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2581 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2582 else if (PyObject_HasAttrString(other, "timetuple")) { |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2583 /* A hook for other kinds of date objects. */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2584 Py_INCREF(Py_NotImplemented); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2585 return Py_NotImplemented; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2586 } |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2587 else if (op == Py_EQ || op == Py_NE) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2588 diff = 1; /* any non-zero value will do */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2589 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2590 else /* stop this from falling back to address comparison */ |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2591 return cmperror((PyObject *)self, other); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
2592 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2593 return diff_to_bool(diff, op); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2594 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2595 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2596 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2597 date_timetuple(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2598 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2599 return build_struct_time(GET_YEAR(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2600 GET_MONTH(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2601 GET_DAY(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2602 0, 0, 0, -1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2603 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2604 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2605 static PyObject * |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2606 date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2607 { |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2608 PyObject *clone; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2609 PyObject *tuple; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2610 int year = GET_YEAR(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2611 int month = GET_MONTH(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2612 int day = GET_DAY(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2613 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2614 if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2615 &year, &month, &day)) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2616 return NULL; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2617 tuple = Py_BuildValue("iii", year, month, day); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2618 if (tuple == NULL) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2619 return NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2620 clone = date_new(Py_TYPE(self), tuple, NULL); |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2621 Py_DECREF(tuple); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2622 return clone; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2623 } |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2624 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2625 static PyObject *date_getstate(PyDateTime_Date *self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2626 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2627 static long |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2628 date_hash(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2629 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2630 if (self->hashcode == -1) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2631 PyObject *temp = date_getstate(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2632 if (temp != NULL) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2633 self->hashcode = PyObject_Hash(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2634 Py_DECREF(temp); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2635 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2636 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2637 return self->hashcode; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2638 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2639 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2640 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2641 date_toordinal(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2642 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2643 return PyInt_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2644 GET_DAY(self))); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2645 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2646 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2647 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2648 date_weekday(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2649 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2650 int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2651 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2652 return PyInt_FromLong(dow); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2653 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2654 |
|
27572
f235f135f3f8
All over: changed comments to reflect pickling is straightforward now,
Tim Peters <tim.peters@gmail.com>
parents:
27571
diff
changeset
|
2655 /* Pickle support, a simple use of __reduce__. */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2656 |
|
27575
3b9fb57fac34
There's no good reason for datetime objects to expose __getstate__()
Tim Peters <tim.peters@gmail.com>
parents:
27573
diff
changeset
|
2657 /* __getstate__ isn't exposed */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2658 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2659 date_getstate(PyDateTime_Date *self) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2660 { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2661 return Py_BuildValue( |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2662 "(N)", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
2663 PyString_FromStringAndSize((char *)self->data, |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2664 _PyDateTime_DATE_DATASIZE)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2665 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2666 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2667 static PyObject * |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2668 date_reduce(PyDateTime_Date *self, PyObject *arg) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2669 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2670 return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2671 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2672 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2673 static PyMethodDef date_methods[] = { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2674 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2675 /* Class methods: */ |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2676 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2677 {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2678 METH_CLASS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2679 PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2680 "time.time()).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2681 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2682 {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2683 METH_CLASS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2684 PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2685 "ordinal.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2686 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2687 {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2688 PyDoc_STR("Current date or datetime: same as " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2689 "self.__class__.fromtimestamp(time.time()).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2690 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2691 /* Instance methods: */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2692 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2693 {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2694 PyDoc_STR("Return ctime() style string.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2695 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
2696 {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2697 PyDoc_STR("format -> strftime() style string.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2698 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2699 {"__format__", (PyCFunction)date_format, METH_VARARGS, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2700 PyDoc_STR("Formats self with strftime.")}, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
2701 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2702 {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2703 PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2704 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2705 {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2706 PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2707 "weekday.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2708 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2709 {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2710 PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2711 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2712 {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2713 PyDoc_STR("Return the day of the week represented by the date.\n" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2714 "Monday == 1 ... Sunday == 7")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2715 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2716 {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2717 PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2718 "1 is day 1.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2719 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2720 {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2721 PyDoc_STR("Return the day of the week represented by the date.\n" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2722 "Monday == 0 ... Sunday == 6")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2723 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
2724 {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2725 PyDoc_STR("Return date with new specified fields.")}, |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
2726 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2727 {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2728 PyDoc_STR("__reduce__() -> (cls, state)")}, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2729 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2730 {NULL, NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2731 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2732 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2733 static char date_doc[] = |
|
34191
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
2734 PyDoc_STR("date(year, month, day) --> date object"); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2735 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2736 static PyNumberMethods date_as_number = { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2737 date_add, /* nb_add */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2738 date_subtract, /* nb_subtract */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2739 0, /* nb_multiply */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2740 0, /* nb_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2741 0, /* nb_remainder */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2742 0, /* nb_divmod */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2743 0, /* nb_power */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2744 0, /* nb_negative */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2745 0, /* nb_positive */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2746 0, /* nb_absolute */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2747 0, /* nb_nonzero */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2748 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2749 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2750 static PyTypeObject PyDateTime_DateType = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41816
diff
changeset
|
2751 PyVarObject_HEAD_INIT(NULL, 0) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2752 "datetime.date", /* tp_name */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2753 sizeof(PyDateTime_Date), /* tp_basicsize */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2754 0, /* tp_itemsize */ |
|
28609
6952373e8fe5
The date class is now properly subclassable. (SF bug #720908)
Guido van Rossum <guido@python.org>
parents:
27765
diff
changeset
|
2755 0, /* tp_dealloc */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2756 0, /* tp_print */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2757 0, /* tp_getattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2758 0, /* tp_setattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2759 0, /* tp_compare */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2760 (reprfunc)date_repr, /* tp_repr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2761 &date_as_number, /* tp_as_number */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2762 0, /* tp_as_sequence */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2763 0, /* tp_as_mapping */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2764 (hashfunc)date_hash, /* tp_hash */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2765 0, /* tp_call */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2766 (reprfunc)date_str, /* tp_str */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2767 PyObject_GenericGetAttr, /* tp_getattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2768 0, /* tp_setattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2769 0, /* tp_as_buffer */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2770 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2771 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2772 date_doc, /* tp_doc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2773 0, /* tp_traverse */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2774 0, /* tp_clear */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2775 (richcmpfunc)date_richcompare, /* tp_richcompare */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2776 0, /* tp_weaklistoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2777 0, /* tp_iter */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2778 0, /* tp_iternext */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2779 date_methods, /* tp_methods */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2780 0, /* tp_members */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2781 date_getset, /* tp_getset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2782 0, /* tp_base */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2783 0, /* tp_dict */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2784 0, /* tp_descr_get */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2785 0, /* tp_descr_set */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2786 0, /* tp_dictoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2787 0, /* tp_init */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2788 0, /* tp_alloc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2789 date_new, /* tp_new */ |
|
29138
c310f9ddfdcb
Turns out there wasn't a need to define tp_free for any of the types here.
Tim Peters <tim.peters@gmail.com>
parents:
29136
diff
changeset
|
2790 0, /* tp_free */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2791 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2792 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2793 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2794 * PyDateTime_TZInfo implementation. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2795 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2796 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2797 /* This is a pure abstract base class, so doesn't do anything beyond |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2798 * raising NotImplemented exceptions. Real tzinfo classes need |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2799 * to derive from this. This is mostly for clarity, and for efficiency in |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
2800 * datetime and time constructors (their tzinfo arguments need to |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2801 * be subclasses of this tzinfo class, which is easy and quick to check). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2802 * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2803 * Note: For reasons having to do with pickling of subclasses, we have |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2804 * to allow tzinfo objects to be instantiated. This wasn't an issue |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2805 * in the Python implementation (__init__() could raise NotImplementedError |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2806 * there without ill effect), but doing so in the C implementation hit a |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2807 * brick wall. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2808 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2809 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2810 static PyObject * |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2811 tzinfo_nogo(const char* methodname) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2812 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2813 PyErr_Format(PyExc_NotImplementedError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2814 "a tzinfo subclass must implement %s()", |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2815 methodname); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2816 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2817 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2818 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2819 /* Methods. A subclass must implement these. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2820 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2821 static PyObject * |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2822 tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2823 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2824 return tzinfo_nogo("tzname"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2825 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2826 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2827 static PyObject * |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2828 tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2829 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2830 return tzinfo_nogo("utcoffset"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2831 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2832 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2833 static PyObject * |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2834 tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2835 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2836 return tzinfo_nogo("dst"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2837 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2838 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2839 static PyObject * |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2840 tzinfo_fromutc(PyDateTime_TZInfo *self, PyDateTime_DateTime *dt) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2841 { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2842 int y, m, d, hh, mm, ss, us; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2843 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2844 PyObject *result; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2845 int off, dst; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2846 int none; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2847 int delta; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2848 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2849 if (! PyDateTime_Check(dt)) { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2850 PyErr_SetString(PyExc_TypeError, |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2851 "fromutc: argument must be a datetime"); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2852 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2853 } |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2854 if (! HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2855 PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2856 "is not self"); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2857 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2858 } |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2859 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2860 off = call_utcoffset(dt->tzinfo, (PyObject *)dt, &none); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2861 if (off == -1 && PyErr_Occurred()) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2862 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2863 if (none) { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2864 PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2865 "utcoffset() result required"); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2866 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2867 } |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2868 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2869 dst = call_dst(dt->tzinfo, (PyObject *)dt, &none); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2870 if (dst == -1 && PyErr_Occurred()) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2871 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2872 if (none) { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2873 PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2874 "dst() result required"); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2875 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2876 } |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2877 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2878 y = GET_YEAR(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2879 m = GET_MONTH(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2880 d = GET_DAY(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2881 hh = DATE_GET_HOUR(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2882 mm = DATE_GET_MINUTE(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2883 ss = DATE_GET_SECOND(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2884 us = DATE_GET_MICROSECOND(dt); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2885 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2886 delta = off - dst; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2887 mm += delta; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2888 if ((mm < 0 || mm >= 60) && |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2889 normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
|
27347
30da8c96fd29
fromutc(): Repair incorrect failure return, as noted by NealN. Thanks!
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2890 return NULL; |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2891 result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2892 if (result == NULL) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2893 return result; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2894 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2895 dst = call_dst(dt->tzinfo, result, &none); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2896 if (dst == -1 && PyErr_Occurred()) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2897 goto Fail; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2898 if (none) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2899 goto Inconsistent; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2900 if (dst == 0) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2901 return result; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2902 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2903 mm += dst; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2904 if ((mm < 0 || mm >= 60) && |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2905 normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2906 goto Fail; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2907 Py_DECREF(result); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2908 result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2909 return result; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2910 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2911 Inconsistent: |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2912 PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2913 "inconsistent results; cannot convert"); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2914 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2915 /* fall thru to failure */ |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2916 Fail: |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2917 Py_DECREF(result); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2918 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2919 } |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2920 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2921 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2922 * Pickle support. This is solely so that tzinfo subclasses can use |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2923 * pickling -- tzinfo itself is supposed to be uninstantiable. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2924 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2925 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2926 static PyObject * |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2927 tzinfo_reduce(PyObject *self) |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2928 { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2929 PyObject *args, *state, *tmp; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2930 PyObject *getinitargs, *getstate; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2931 |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2932 tmp = PyTuple_New(0); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2933 if (tmp == NULL) |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2934 return NULL; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2935 |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2936 getinitargs = PyObject_GetAttrString(self, "__getinitargs__"); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2937 if (getinitargs != NULL) { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2938 args = PyObject_CallObject(getinitargs, tmp); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2939 Py_DECREF(getinitargs); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2940 if (args == NULL) { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2941 Py_DECREF(tmp); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2942 return NULL; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2943 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2944 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2945 else { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2946 PyErr_Clear(); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2947 args = tmp; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2948 Py_INCREF(args); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2949 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2950 |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2951 getstate = PyObject_GetAttrString(self, "__getstate__"); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2952 if (getstate != NULL) { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2953 state = PyObject_CallObject(getstate, tmp); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2954 Py_DECREF(getstate); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2955 if (state == NULL) { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2956 Py_DECREF(args); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2957 Py_DECREF(tmp); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2958 return NULL; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2959 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2960 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2961 else { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2962 PyObject **dictptr; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2963 PyErr_Clear(); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2964 state = Py_None; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2965 dictptr = _PyObject_GetDictPtr(self); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2966 if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2967 state = *dictptr; |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2968 Py_INCREF(state); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2969 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2970 |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2971 Py_DECREF(tmp); |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2972 |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2973 if (state == Py_None) { |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2974 Py_DECREF(state); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2975 return Py_BuildValue("(ON)", Py_TYPE(self), args); |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2976 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2977 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
2978 return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2979 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2980 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2981 static PyMethodDef tzinfo_methods[] = { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2982 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2983 {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2984 PyDoc_STR("datetime -> string name of time zone.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2985 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2986 {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2987 PyDoc_STR("datetime -> minutes east of UTC (negative for " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2988 "west of UTC).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2989 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2990 {"dst", (PyCFunction)tzinfo_dst, METH_O, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2991 PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2992 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2993 {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2994 PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2995 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2996 {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2997 PyDoc_STR("-> (cls, state)")}, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2998 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2999 {NULL, NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3000 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3001 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3002 static char tzinfo_doc[] = |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3003 PyDoc_STR("Abstract base class for time zone info objects."); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3004 |
|
27682
74aabd4419d4
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
27576
diff
changeset
|
3005 statichere PyTypeObject PyDateTime_TZInfoType = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3006 PyObject_HEAD_INIT(NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3007 0, /* ob_size */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3008 "datetime.tzinfo", /* tp_name */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3009 sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3010 0, /* tp_itemsize */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3011 0, /* tp_dealloc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3012 0, /* tp_print */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3013 0, /* tp_getattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3014 0, /* tp_setattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3015 0, /* tp_compare */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3016 0, /* tp_repr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3017 0, /* tp_as_number */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3018 0, /* tp_as_sequence */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3019 0, /* tp_as_mapping */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3020 0, /* tp_hash */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3021 0, /* tp_call */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3022 0, /* tp_str */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3023 PyObject_GenericGetAttr, /* tp_getattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3024 0, /* tp_setattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3025 0, /* tp_as_buffer */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3026 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3027 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3028 tzinfo_doc, /* tp_doc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3029 0, /* tp_traverse */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3030 0, /* tp_clear */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3031 0, /* tp_richcompare */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3032 0, /* tp_weaklistoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3033 0, /* tp_iter */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3034 0, /* tp_iternext */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3035 tzinfo_methods, /* tp_methods */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3036 0, /* tp_members */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3037 0, /* tp_getset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3038 0, /* tp_base */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3039 0, /* tp_dict */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3040 0, /* tp_descr_get */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3041 0, /* tp_descr_set */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3042 0, /* tp_dictoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3043 0, /* tp_init */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3044 0, /* tp_alloc */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3045 PyType_GenericNew, /* tp_new */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3046 0, /* tp_free */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3047 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3048 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3049 /* |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3050 * PyDateTime_Time implementation. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3051 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3052 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3053 /* Accessor properties. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3054 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3055 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3056 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3057 time_hour(PyDateTime_Time *self, void *unused) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3058 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3059 return PyInt_FromLong(TIME_GET_HOUR(self)); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3060 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3061 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3062 static PyObject * |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3063 time_minute(PyDateTime_Time *self, void *unused) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3064 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3065 return PyInt_FromLong(TIME_GET_MINUTE(self)); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3066 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3067 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3068 /* The name time_second conflicted with some platform header file. */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3069 static PyObject * |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3070 py_time_second(PyDateTime_Time *self, void *unused) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3071 { |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3072 return PyInt_FromLong(TIME_GET_SECOND(self)); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3073 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3074 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3075 static PyObject * |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3076 time_microsecond(PyDateTime_Time *self, void *unused) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3077 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3078 return PyInt_FromLong(TIME_GET_MICROSECOND(self)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3079 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3080 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3081 static PyObject * |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3082 time_tzinfo(PyDateTime_Time *self, void *unused) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3083 { |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3084 PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3085 Py_INCREF(result); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3086 return result; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3087 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3088 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3089 static PyGetSetDef time_getset[] = { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3090 {"hour", (getter)time_hour}, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3091 {"minute", (getter)time_minute}, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3092 {"second", (getter)py_time_second}, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3093 {"microsecond", (getter)time_microsecond}, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3094 {"tzinfo", (getter)time_tzinfo}, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3095 {NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3096 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3097 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3098 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3099 * Constructors. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3100 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3101 |
| 36684 | 3102 static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3103 "tzinfo", NULL}; |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3104 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3105 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3106 time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3107 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3108 PyObject *self = NULL; |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3109 PyObject *state; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3110 int hour = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3111 int minute = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3112 int second = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3113 int usecond = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3114 PyObject *tzinfo = Py_None; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3115 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3116 /* Check for invocation from pickle with __getstate__ state */ |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3117 if (PyTuple_GET_SIZE(args) >= 1 && |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3118 PyTuple_GET_SIZE(args) <= 2 && |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3119 PyString_Check(state = PyTuple_GET_ITEM(args, 0)) && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3120 PyString_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3121 ((unsigned char) (PyString_AS_STRING(state)[0])) < 24) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3122 { |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3123 PyDateTime_Time *me; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3124 char aware; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3125 |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3126 if (PyTuple_GET_SIZE(args) == 2) { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3127 tzinfo = PyTuple_GET_ITEM(args, 1); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3128 if (check_tzinfo_subclass(tzinfo) < 0) { |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3129 PyErr_SetString(PyExc_TypeError, "bad " |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3130 "tzinfo state arg"); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3131 return NULL; |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3132 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3133 } |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3134 aware = (char)(tzinfo != Py_None); |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
3135 me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3136 if (me != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3137 char *pdata = PyString_AS_STRING(state); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3138 |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3139 memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3140 me->hashcode = -1; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3141 me->hastzinfo = aware; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3142 if (aware) { |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3143 Py_INCREF(tzinfo); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3144 me->tzinfo = tzinfo; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3145 } |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3146 } |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3147 return (PyObject *)me; |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3148 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3149 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3150 if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3151 &hour, &minute, &second, &usecond, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3152 &tzinfo)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3153 if (check_time_args(hour, minute, second, usecond) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3154 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3155 if (check_tzinfo_subclass(tzinfo) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3156 return NULL; |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
3157 self = new_time_ex(hour, minute, second, usecond, tzinfo, |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
3158 type); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3159 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3160 return self; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3161 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3162 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3163 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3164 * Destructor. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3165 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3166 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3167 static void |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3168 time_dealloc(PyDateTime_Time *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3169 { |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3170 if (HASTZINFO(self)) { |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3171 Py_XDECREF(self->tzinfo); |
|
27199
77b70a8884cb
Get rid of compiler warnings
Neal Norwitz <nnorwitz@gmail.com>
parents:
27195
diff
changeset
|
3172 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
3173 Py_TYPE(self)->tp_free((PyObject *)self); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3174 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3175 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3176 /* |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
3177 * Indirect access to tzinfo methods. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3178 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3179 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3180 /* These are all METH_NOARGS, so don't need to check the arglist. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3181 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3182 time_utcoffset(PyDateTime_Time *self, PyObject *unused) { |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3183 return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3184 "utcoffset", Py_None); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
3185 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
3186 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
3187 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3188 time_dst(PyDateTime_Time *self, PyObject *unused) { |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3189 return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3190 "dst", Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3191 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3192 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3193 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3194 time_tzname(PyDateTime_Time *self, PyObject *unused) { |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3195 return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None, |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3196 Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3197 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3198 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3199 /* |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3200 * Various ways to turn a time into a string. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3201 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3202 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3203 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3204 time_repr(PyDateTime_Time *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3205 { |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3206 char buffer[100]; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
3207 const char *type_name = Py_TYPE(self)->tp_name; |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3208 int h = TIME_GET_HOUR(self); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3209 int m = TIME_GET_MINUTE(self); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3210 int s = TIME_GET_SECOND(self); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3211 int us = TIME_GET_MICROSECOND(self); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3212 PyObject *result = NULL; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3213 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3214 if (us) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3215 PyOS_snprintf(buffer, sizeof(buffer), |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
3216 "%s(%d, %d, %d, %d)", type_name, h, m, s, us); |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3217 else if (s) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3218 PyOS_snprintf(buffer, sizeof(buffer), |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
3219 "%s(%d, %d, %d)", type_name, h, m, s); |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3220 else |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3221 PyOS_snprintf(buffer, sizeof(buffer), |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
3222 "%s(%d, %d)", type_name, h, m); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3223 result = PyString_FromString(buffer); |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3224 if (result != NULL && HASTZINFO(self)) |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3225 result = append_keyword_tzinfo(result, self->tzinfo); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3226 return result; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3227 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3228 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3229 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3230 time_str(PyDateTime_Time *self) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3231 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3232 return PyObject_CallMethod((PyObject *)self, "isoformat", "()"); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3233 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3234 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3235 static PyObject * |
|
40771
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
3236 time_isoformat(PyDateTime_Time *self, PyObject *unused) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3237 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3238 char buf[100]; |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3239 PyObject *result; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3240 /* Reuse the time format code from the datetime type. */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3241 PyDateTime_DateTime datetime; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3242 PyDateTime_DateTime *pdatetime = &datetime; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3243 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3244 /* Copy over just the time bytes. */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3245 memcpy(pdatetime->data + _PyDateTime_DATE_DATASIZE, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3246 self->data, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3247 _PyDateTime_TIME_DATASIZE); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3248 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3249 isoformat_time(pdatetime, buf, sizeof(buf)); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3250 result = PyString_FromString(buf); |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3251 if (result == NULL || ! HASTZINFO(self) || self->tzinfo == Py_None) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3252 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3253 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3254 /* We need to append the UTC offset. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3255 if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
3256 Py_None) < 0) { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3257 Py_DECREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3258 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3259 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3260 PyString_ConcatAndDel(&result, PyString_FromString(buf)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3261 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3262 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3263 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3264 static PyObject * |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3265 time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3266 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3267 PyObject *result; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3268 PyObject *tuple; |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
3269 const char *format; |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
3270 Py_ssize_t format_len; |
| 36684 | 3271 static char *keywords[] = {"format", NULL}; |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3272 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
3273 if (! PyArg_ParseTupleAndKeywords(args, kw, "s#:strftime", keywords, |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
3274 &format, &format_len)) |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3275 return NULL; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3276 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3277 /* Python's strftime does insane things with the year part of the |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3278 * timetuple. The year is forced to (the otherwise nonsensical) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3279 * 1900 to worm around that. |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3280 */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3281 tuple = Py_BuildValue("iiiiiiiii", |
|
31608
ae8dbf500172
Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents:
30722
diff
changeset
|
3282 1900, 1, 1, /* year, month, day */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3283 TIME_GET_HOUR(self), |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3284 TIME_GET_MINUTE(self), |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3285 TIME_GET_SECOND(self), |
|
31608
ae8dbf500172
Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents:
30722
diff
changeset
|
3286 0, 1, -1); /* weekday, daynum, dst */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3287 if (tuple == NULL) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3288 return NULL; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3289 assert(PyTuple_Size(tuple) == 9); |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
3290 result = wrap_strftime((PyObject *)self, format, format_len, tuple, |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
3291 Py_None); |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3292 Py_DECREF(tuple); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3293 return result; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3294 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3295 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3296 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3297 * Miscellaneous methods. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3298 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3299 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3300 /* This is more natural as a tp_compare, but doesn't work then: for whatever |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3301 * reason, Python's try_3way_compare ignores tp_compare unless |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3302 * PyInstance_Check returns true, but these aren't old-style classes. |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3303 */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3304 static PyObject * |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3305 time_richcompare(PyDateTime_Time *self, PyObject *other, int op) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3306 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3307 int diff; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3308 naivety n1, n2; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3309 int offset1, offset2; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3310 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3311 if (! PyTime_Check(other)) { |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
3312 if (op == Py_EQ || op == Py_NE) { |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
3313 PyObject *result = op == Py_EQ ? Py_False : Py_True; |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
3314 Py_INCREF(result); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
3315 return result; |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
3316 } |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3317 /* Stop this from falling back to address comparison. */ |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
3318 return cmperror((PyObject *)self, other); |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3319 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3320 if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1, Py_None, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3321 other, &offset2, &n2, Py_None) < 0) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3322 return NULL; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3323 assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3324 /* If they're both naive, or both aware and have the same offsets, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3325 * we get off cheap. Note that if they're both naive, offset1 == |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3326 * offset2 == 0 at this point. |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3327 */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3328 if (n1 == n2 && offset1 == offset2) { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3329 diff = memcmp(self->data, ((PyDateTime_Time *)other)->data, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3330 _PyDateTime_TIME_DATASIZE); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3331 return diff_to_bool(diff, op); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3332 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3333 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3334 if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3335 assert(offset1 != offset2); /* else last "if" handled it */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3336 /* Convert everything except microseconds to seconds. These |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3337 * can't overflow (no more than the # of seconds in 2 days). |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3338 */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3339 offset1 = TIME_GET_HOUR(self) * 3600 + |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3340 (TIME_GET_MINUTE(self) - offset1) * 60 + |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3341 TIME_GET_SECOND(self); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3342 offset2 = TIME_GET_HOUR(other) * 3600 + |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3343 (TIME_GET_MINUTE(other) - offset2) * 60 + |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3344 TIME_GET_SECOND(other); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3345 diff = offset1 - offset2; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3346 if (diff == 0) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3347 diff = TIME_GET_MICROSECOND(self) - |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3348 TIME_GET_MICROSECOND(other); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3349 return diff_to_bool(diff, op); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3350 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3351 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3352 assert(n1 != n2); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3353 PyErr_SetString(PyExc_TypeError, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3354 "can't compare offset-naive and " |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3355 "offset-aware times"); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3356 return NULL; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3357 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3358 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3359 static long |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3360 time_hash(PyDateTime_Time *self) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3361 { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3362 if (self->hashcode == -1) { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3363 naivety n; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3364 int offset; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3365 PyObject *temp; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3366 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3367 n = classify_utcoffset((PyObject *)self, Py_None, &offset); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3368 assert(n != OFFSET_UNKNOWN); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3369 if (n == OFFSET_ERROR) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3370 return -1; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3371 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3372 /* Reduce this to a hash of another object. */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3373 if (offset == 0) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3374 temp = PyString_FromStringAndSize((char *)self->data, |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3375 _PyDateTime_TIME_DATASIZE); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3376 else { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3377 int hour; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3378 int minute; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3379 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3380 assert(n == OFFSET_AWARE); |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3381 assert(HASTZINFO(self)); |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3382 hour = divmod(TIME_GET_HOUR(self) * 60 + |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3383 TIME_GET_MINUTE(self) - offset, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3384 60, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3385 &minute); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3386 if (0 <= hour && hour < 24) |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3387 temp = new_time(hour, minute, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3388 TIME_GET_SECOND(self), |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3389 TIME_GET_MICROSECOND(self), |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3390 Py_None); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3391 else |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3392 temp = Py_BuildValue("iiii", |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3393 hour, minute, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3394 TIME_GET_SECOND(self), |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3395 TIME_GET_MICROSECOND(self)); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3396 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3397 if (temp != NULL) { |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3398 self->hashcode = PyObject_Hash(temp); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3399 Py_DECREF(temp); |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3400 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3401 } |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3402 return self->hashcode; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3403 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3404 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3405 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3406 time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3407 { |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3408 PyObject *clone; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3409 PyObject *tuple; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3410 int hh = TIME_GET_HOUR(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3411 int mm = TIME_GET_MINUTE(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3412 int ss = TIME_GET_SECOND(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3413 int us = TIME_GET_MICROSECOND(self); |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3414 PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3415 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3416 if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3417 time_kws, |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3418 &hh, &mm, &ss, &us, &tzinfo)) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3419 return NULL; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3420 tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3421 if (tuple == NULL) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3422 return NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
3423 clone = time_new(Py_TYPE(self), tuple, NULL); |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3424 Py_DECREF(tuple); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3425 return clone; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3426 } |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3427 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3428 static int |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3429 time_nonzero(PyDateTime_Time *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3430 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3431 int offset; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3432 int none; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3433 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3434 if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3435 /* Since utcoffset is in whole minutes, nothing can |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3436 * alter the conclusion that this is nonzero. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3437 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3438 return 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3439 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3440 offset = 0; |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3441 if (HASTZINFO(self) && self->tzinfo != Py_None) { |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26809
diff
changeset
|
3442 offset = call_utcoffset(self->tzinfo, Py_None, &none); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3443 if (offset == -1 && PyErr_Occurred()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3444 return -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3445 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3446 return (TIME_GET_MINUTE(self) - offset + TIME_GET_HOUR(self)*60) != 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3447 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3448 |
|
27572
f235f135f3f8
All over: changed comments to reflect pickling is straightforward now,
Tim Peters <tim.peters@gmail.com>
parents:
27571
diff
changeset
|
3449 /* Pickle support, a simple use of __reduce__. */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3450 |
|
27193
934732787419
Removed more now-pointless pickle code.
Tim Peters <tim.peters@gmail.com>
parents:
27172
diff
changeset
|
3451 /* Let basestate be the non-tzinfo data string. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3452 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3453 * So it's a tuple in any (non-error) case. |
|
27575
3b9fb57fac34
There's no good reason for datetime objects to expose __getstate__()
Tim Peters <tim.peters@gmail.com>
parents:
27573
diff
changeset
|
3454 * __getstate__ isn't exposed. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3455 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3456 static PyObject * |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3457 time_getstate(PyDateTime_Time *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3458 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3459 PyObject *basestate; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3460 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3461 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3462 basestate = PyString_FromStringAndSize((char *)self->data, |
|
27193
934732787419
Removed more now-pointless pickle code.
Tim Peters <tim.peters@gmail.com>
parents:
27172
diff
changeset
|
3463 _PyDateTime_TIME_DATASIZE); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3464 if (basestate != NULL) { |
|
27217
10b7e60245b5
Minor fiddling to make the next part easier. Introduced an internal
Tim Peters <tim.peters@gmail.com>
parents:
27199
diff
changeset
|
3465 if (! HASTZINFO(self) || self->tzinfo == Py_None) |
|
30683
3b59fa5d25b1
Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents:
29601
diff
changeset
|
3466 result = PyTuple_Pack(1, basestate); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3467 else |
|
30683
3b59fa5d25b1
Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents:
29601
diff
changeset
|
3468 result = PyTuple_Pack(2, basestate, self->tzinfo); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3469 Py_DECREF(basestate); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3470 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3471 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3472 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3473 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3474 static PyObject * |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3475 time_reduce(PyDateTime_Time *self, PyObject *arg) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3476 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
3477 return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3478 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3479 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3480 static PyMethodDef time_methods[] = { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3481 |
|
40771
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
3482 {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3483 PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3484 "[+HH:MM].")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3485 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
3486 {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3487 PyDoc_STR("format -> strftime() style string.")}, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3488 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
3489 {"__format__", (PyCFunction)date_format, METH_VARARGS, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
3490 PyDoc_STR("Formats self with strftime.")}, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44439
diff
changeset
|
3491 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3492 {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3493 PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3494 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3495 {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3496 PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3497 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3498 {"dst", (PyCFunction)time_dst, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3499 PyDoc_STR("Return self.tzinfo.dst(self).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3500 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
3501 {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3502 PyDoc_STR("Return time with new specified fields.")}, |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3503 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3504 {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3505 PyDoc_STR("__reduce__() -> (cls, state)")}, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3506 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3507 {NULL, NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3508 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3509 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3510 static char time_doc[] = |
|
34191
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
3511 PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
|
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
3512 \n\ |
|
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
3513 All arguments are optional. tzinfo may be None, or an instance of\n\ |
|
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
3514 a tzinfo subclass. The remaining arguments may be ints or longs.\n"); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3515 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3516 static PyNumberMethods time_as_number = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3517 0, /* nb_add */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3518 0, /* nb_subtract */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3519 0, /* nb_multiply */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3520 0, /* nb_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3521 0, /* nb_remainder */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3522 0, /* nb_divmod */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3523 0, /* nb_power */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3524 0, /* nb_negative */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3525 0, /* nb_positive */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3526 0, /* nb_absolute */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3527 (inquiry)time_nonzero, /* nb_nonzero */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3528 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3529 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3530 statichere PyTypeObject PyDateTime_TimeType = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3531 PyObject_HEAD_INIT(NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3532 0, /* ob_size */ |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27159
diff
changeset
|
3533 "datetime.time", /* tp_name */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3534 sizeof(PyDateTime_Time), /* tp_basicsize */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3535 0, /* tp_itemsize */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3536 (destructor)time_dealloc, /* tp_dealloc */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3537 0, /* tp_print */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3538 0, /* tp_getattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3539 0, /* tp_setattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3540 0, /* tp_compare */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3541 (reprfunc)time_repr, /* tp_repr */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3542 &time_as_number, /* tp_as_number */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3543 0, /* tp_as_sequence */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3544 0, /* tp_as_mapping */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3545 (hashfunc)time_hash, /* tp_hash */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3546 0, /* tp_call */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3547 (reprfunc)time_str, /* tp_str */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3548 PyObject_GenericGetAttr, /* tp_getattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3549 0, /* tp_setattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3550 0, /* tp_as_buffer */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3551 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3552 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3553 time_doc, /* tp_doc */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3554 0, /* tp_traverse */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3555 0, /* tp_clear */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3556 (richcmpfunc)time_richcompare, /* tp_richcompare */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3557 0, /* tp_weaklistoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3558 0, /* tp_iter */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3559 0, /* tp_iternext */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3560 time_methods, /* tp_methods */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3561 0, /* tp_members */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3562 time_getset, /* tp_getset */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3563 0, /* tp_base */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3564 0, /* tp_dict */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3565 0, /* tp_descr_get */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3566 0, /* tp_descr_set */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3567 0, /* tp_dictoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3568 0, /* tp_init */ |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
3569 time_alloc, /* tp_alloc */ |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
3570 time_new, /* tp_new */ |
|
29138
c310f9ddfdcb
Turns out there wasn't a need to define tp_free for any of the types here.
Tim Peters <tim.peters@gmail.com>
parents:
29136
diff
changeset
|
3571 0, /* tp_free */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3572 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3573 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3574 /* |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3575 * PyDateTime_DateTime implementation. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3576 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3577 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3578 /* Accessor properties. Properties for day, month, and year are inherited |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3579 * from date. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3580 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3581 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3582 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3583 datetime_hour(PyDateTime_DateTime *self, void *unused) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3584 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3585 return PyInt_FromLong(DATE_GET_HOUR(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3586 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3587 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3588 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3589 datetime_minute(PyDateTime_DateTime *self, void *unused) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3590 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3591 return PyInt_FromLong(DATE_GET_MINUTE(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3592 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3593 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3594 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3595 datetime_second(PyDateTime_DateTime *self, void *unused) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3596 { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3597 return PyInt_FromLong(DATE_GET_SECOND(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3598 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3599 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3600 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3601 datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3602 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3603 return PyInt_FromLong(DATE_GET_MICROSECOND(self)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3604 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3605 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3606 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3607 datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3608 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3609 PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3610 Py_INCREF(result); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3611 return result; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3612 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3613 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3614 static PyGetSetDef datetime_getset[] = { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3615 {"hour", (getter)datetime_hour}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3616 {"minute", (getter)datetime_minute}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3617 {"second", (getter)datetime_second}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3618 {"microsecond", (getter)datetime_microsecond}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3619 {"tzinfo", (getter)datetime_tzinfo}, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3620 {NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3621 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3622 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3623 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3624 * Constructors. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3625 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3626 |
| 36684 | 3627 static char *datetime_kws[] = { |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3628 "year", "month", "day", "hour", "minute", "second", |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3629 "microsecond", "tzinfo", NULL |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3630 }; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
3631 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3632 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3633 datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3634 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3635 PyObject *self = NULL; |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3636 PyObject *state; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3637 int year; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3638 int month; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3639 int day; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3640 int hour = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3641 int minute = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3642 int second = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3643 int usecond = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3644 PyObject *tzinfo = Py_None; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3645 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3646 /* Check for invocation from pickle with __getstate__ state */ |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3647 if (PyTuple_GET_SIZE(args) >= 1 && |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3648 PyTuple_GET_SIZE(args) <= 2 && |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3649 PyString_Check(state = PyTuple_GET_ITEM(args, 0)) && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3650 PyString_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3651 MONTH_IS_SANE(PyString_AS_STRING(state)[2])) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3652 { |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3653 PyDateTime_DateTime *me; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3654 char aware; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3655 |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3656 if (PyTuple_GET_SIZE(args) == 2) { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3657 tzinfo = PyTuple_GET_ITEM(args, 1); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3658 if (check_tzinfo_subclass(tzinfo) < 0) { |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3659 PyErr_SetString(PyExc_TypeError, "bad " |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3660 "tzinfo state arg"); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3661 return NULL; |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3662 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3663 } |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3664 aware = (char)(tzinfo != Py_None); |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
3665 me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3666 if (me != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
3667 char *pdata = PyString_AS_STRING(state); |
|
27576
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3668 |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3669 memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3670 me->hashcode = -1; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3671 me->hastzinfo = aware; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3672 if (aware) { |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3673 Py_INCREF(tzinfo); |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3674 me->tzinfo = tzinfo; |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3675 } |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3676 } |
|
1608507bcc3f
New functions alloc_{time,datetime}. Got rid of all setstate-like
Tim Peters <tim.peters@gmail.com>
parents:
27575
diff
changeset
|
3677 return (PyObject *)me; |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3678 } |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
3679 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3680 if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3681 &year, &month, &day, &hour, &minute, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3682 &second, &usecond, &tzinfo)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3683 if (check_date_args(year, month, day) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3684 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3685 if (check_time_args(hour, minute, second, usecond) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3686 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3687 if (check_tzinfo_subclass(tzinfo) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3688 return NULL; |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
3689 self = new_datetime_ex(year, month, day, |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
3690 hour, minute, second, usecond, |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
3691 tzinfo, type); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3692 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3693 return self; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3694 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3695 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3696 /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3697 typedef struct tm *(*TM_FUNC)(const time_t *timer); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3698 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3699 /* Internal helper. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3700 * Build datetime from a time_t and a distinct count of microseconds. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3701 * Pass localtime or gmtime for f, to control the interpretation of timet. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3702 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3703 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3704 datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3705 PyObject *tzinfo) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3706 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3707 struct tm *tm; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3708 PyObject *result = NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3709 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3710 tm = f(&timet); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3711 if (tm) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3712 /* The platform localtime/gmtime may insert leap seconds, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3713 * indicated by tm->tm_sec > 59. We don't care about them, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3714 * except to the extent that passing them on to the datetime |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3715 * constructor would raise ValueError for a reason that |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3716 * made no sense to the user. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3717 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3718 if (tm->tm_sec > 59) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3719 tm->tm_sec = 59; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3720 result = PyObject_CallFunction(cls, "iiiiiiiO", |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3721 tm->tm_year + 1900, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3722 tm->tm_mon + 1, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3723 tm->tm_mday, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3724 tm->tm_hour, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3725 tm->tm_min, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3726 tm->tm_sec, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3727 us, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3728 tzinfo); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3729 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3730 else |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3731 PyErr_SetString(PyExc_ValueError, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3732 "timestamp out of range for " |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3733 "platform localtime()/gmtime() function"); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3734 return result; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3735 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3736 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3737 /* Internal helper. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3738 * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3739 * to control the interpretation of the timestamp. Since a double doesn't |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3740 * have enough bits to cover a datetime's full range of precision, it's |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3741 * better to call datetime_from_timet_and_us provided you have a way |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3742 * to get that much precision (e.g., C time() isn't good enough). |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3743 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3744 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3745 datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3746 PyObject *tzinfo) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3747 { |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3748 time_t timet; |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3749 double fraction; |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3750 int us; |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3751 |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3752 timet = _PyTime_DoubleToTimet(timestamp); |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3753 if (timet == (time_t)-1 && PyErr_Occurred()) |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3754 return NULL; |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3755 fraction = timestamp - (double)timet; |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
3756 us = (int)round_to_long(fraction * 1e6); |
|
41026
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
3757 if (us < 0) { |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
3758 /* Truncation towards zero is not what we wanted |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
3759 for negative numbers (Python's mod semantics) */ |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
3760 timet -= 1; |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
3761 us += 1000000; |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
3762 } |
|
38036
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3763 /* If timestamp is less than one microsecond smaller than a |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3764 * full second, round up. Otherwise, ValueErrors are raised |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3765 * for some floats. */ |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3766 if (us == 1000000) { |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3767 timet += 1; |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3768 us = 0; |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37846
diff
changeset
|
3769 } |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3770 return datetime_from_timet_and_us(cls, f, timet, us, tzinfo); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3771 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3772 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3773 /* Internal helper. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3774 * Build most accurate possible datetime for current time. Pass localtime or |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3775 * gmtime for f as appropriate. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3776 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3777 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3778 datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3779 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3780 #ifdef HAVE_GETTIMEOFDAY |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3781 struct timeval t; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3782 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3783 #ifdef GETTIMEOFDAY_NO_TZ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3784 gettimeofday(&t); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3785 #else |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3786 gettimeofday(&t, (struct timezone *)NULL); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3787 #endif |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3788 return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3789 tzinfo); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3790 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3791 #else /* ! HAVE_GETTIMEOFDAY */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3792 /* No flavor of gettimeofday exists on this platform. Python's |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3793 * time.time() does a lot of other platform tricks to get the |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3794 * best time it can on the platform, and we're not going to do |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3795 * better than that (if we could, the better code would belong |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3796 * in time.time()!) We're limited by the precision of a double, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3797 * though. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3798 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3799 PyObject *time; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3800 double dtime; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3801 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3802 time = time_time(); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3803 if (time == NULL) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3804 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3805 dtime = PyFloat_AsDouble(time); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3806 Py_DECREF(time); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3807 if (dtime == -1.0 && PyErr_Occurred()) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3808 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3809 return datetime_from_timestamp(cls, f, dtime, tzinfo); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3810 #endif /* ! HAVE_GETTIMEOFDAY */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3811 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3812 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3813 /* Return best possible local time -- this isn't constrained by the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3814 * precision of a timestamp. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3815 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3816 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3817 datetime_now(PyObject *cls, PyObject *args, PyObject *kw) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3818 { |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3819 PyObject *self; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3820 PyObject *tzinfo = Py_None; |
| 36684 | 3821 static char *keywords[] = {"tz", NULL}; |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3822 |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3823 if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords, |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3824 &tzinfo)) |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3825 return NULL; |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3826 if (check_tzinfo_subclass(tzinfo) < 0) |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3827 return NULL; |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3828 |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3829 self = datetime_best_possible(cls, |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3830 tzinfo == Py_None ? localtime : gmtime, |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3831 tzinfo); |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3832 if (self != NULL && tzinfo != Py_None) { |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3833 /* Convert UTC to tzinfo's zone. */ |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3834 PyObject *temp = self; |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3835 self = PyObject_CallMethod(tzinfo, "fromutc", "O", self); |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27347
diff
changeset
|
3836 Py_DECREF(temp); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3837 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3838 return self; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3839 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3840 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3841 /* Return best possible UTC time -- this isn't constrained by the |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3842 * precision of a timestamp. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3843 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3844 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3845 datetime_utcnow(PyObject *cls, PyObject *dummy) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3846 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3847 return datetime_best_possible(cls, gmtime, Py_None); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3848 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3849 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3850 /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3851 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3852 datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3853 { |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3854 PyObject *self; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3855 double timestamp; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3856 PyObject *tzinfo = Py_None; |
| 36684 | 3857 static char *keywords[] = {"timestamp", "tz", NULL}; |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3858 |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3859 if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp", |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3860 keywords, ×tamp, &tzinfo)) |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3861 return NULL; |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3862 if (check_tzinfo_subclass(tzinfo) < 0) |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3863 return NULL; |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3864 |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3865 self = datetime_from_timestamp(cls, |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3866 tzinfo == Py_None ? localtime : gmtime, |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3867 timestamp, |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3868 tzinfo); |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3869 if (self != NULL && tzinfo != Py_None) { |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3870 /* Convert UTC to tzinfo's zone. */ |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3871 PyObject *temp = self; |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3872 self = PyObject_CallMethod(tzinfo, "fromutc", "O", self); |
|
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
3873 Py_DECREF(temp); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3874 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3875 return self; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3876 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3877 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3878 /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3879 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3880 datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3881 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3882 double timestamp; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3883 PyObject *result = NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3884 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3885 if (PyArg_ParseTuple(args, "d:utcfromtimestamp", ×tamp)) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3886 result = datetime_from_timestamp(cls, gmtime, timestamp, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3887 Py_None); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3888 return result; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3889 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3890 |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3891 /* Return new datetime from time.strptime(). */ |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3892 static PyObject * |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3893 datetime_strptime(PyObject *cls, PyObject *args) |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3894 { |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3895 static PyObject *module = NULL; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3896 PyObject *result = NULL, *obj, *st = NULL, *frac = NULL; |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3897 const char *string, *format; |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3898 |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3899 if (!PyArg_ParseTuple(args, "ss:strptime", &string, &format)) |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3900 return NULL; |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3901 |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3902 if (module == NULL && |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3903 (module = PyImport_ImportModuleNoBlock("_strptime")) == NULL) |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3904 return NULL; |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3905 |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3906 /* _strptime._strptime returns a two-element tuple. The first |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3907 element is a time.struct_time object. The second is the |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3908 microseconds (which are not defined for time.struct_time). */ |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3909 obj = PyObject_CallMethod(module, "_strptime", "ss", string, format); |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3910 if (obj != NULL) { |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3911 int i, good_timetuple = 1; |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3912 long int ia[7]; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3913 if (PySequence_Check(obj) && PySequence_Size(obj) == 2) { |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3914 st = PySequence_GetItem(obj, 0); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3915 frac = PySequence_GetItem(obj, 1); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3916 if (st == NULL || frac == NULL) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3917 good_timetuple = 0; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3918 /* copy y/m/d/h/m/s values out of the |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3919 time.struct_time */ |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3920 if (good_timetuple && |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3921 PySequence_Check(st) && |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3922 PySequence_Size(st) >= 6) { |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3923 for (i=0; i < 6; i++) { |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3924 PyObject *p = PySequence_GetItem(st, i); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3925 if (p == NULL) { |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3926 good_timetuple = 0; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3927 break; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3928 } |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3929 if (PyInt_Check(p)) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3930 ia[i] = PyInt_AsLong(p); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3931 else |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3932 good_timetuple = 0; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3933 Py_DECREF(p); |
|
37723
b940ac4f9558
Coverity-found bug: datetime_strptime() failed to check for NULL return from
Thomas Wouters <thomas@python.org>
parents:
37088
diff
changeset
|
3934 } |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3935 } |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3936 else |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3937 good_timetuple = 0; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3938 /* follow that up with a little dose of microseconds */ |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3939 if (PyInt_Check(frac)) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3940 ia[6] = PyInt_AsLong(frac); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3941 else |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3942 good_timetuple = 0; |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3943 } |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3944 else |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3945 good_timetuple = 0; |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3946 if (good_timetuple) |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3947 result = PyObject_CallFunction(cls, "iiiiiii", |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3948 ia[0], ia[1], ia[2], |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3949 ia[3], ia[4], ia[5], |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3950 ia[6]); |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3951 else |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3952 PyErr_SetString(PyExc_ValueError, |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3953 "unexpected value from _strptime._strptime"); |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3954 } |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3955 Py_XDECREF(obj); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3956 Py_XDECREF(st); |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45381
diff
changeset
|
3957 Py_XDECREF(frac); |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3958 return result; |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3959 } |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
3960 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3961 /* Return new datetime from date/datetime and time arguments. */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3962 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3963 datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3964 { |
| 36684 | 3965 static char *keywords[] = {"date", "time", NULL}; |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3966 PyObject *date; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3967 PyObject *time; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3968 PyObject *result = NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3969 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3970 if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3971 &PyDateTime_DateType, &date, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3972 &PyDateTime_TimeType, &time)) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3973 PyObject *tzinfo = Py_None; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3974 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3975 if (HASTZINFO(time)) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3976 tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3977 result = PyObject_CallFunction(cls, "iiiiiiiO", |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3978 GET_YEAR(date), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3979 GET_MONTH(date), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3980 GET_DAY(date), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3981 TIME_GET_HOUR(time), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3982 TIME_GET_MINUTE(time), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3983 TIME_GET_SECOND(time), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3984 TIME_GET_MICROSECOND(time), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3985 tzinfo); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3986 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3987 return result; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3988 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3989 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3990 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3991 * Destructor. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3992 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3993 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3994 static void |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3995 datetime_dealloc(PyDateTime_DateTime *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3996 { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3997 if (HASTZINFO(self)) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3998 Py_XDECREF(self->tzinfo); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
3999 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
4000 Py_TYPE(self)->tp_free((PyObject *)self); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4001 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4002 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4003 /* |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4004 * Indirect access to tzinfo methods. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4005 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4006 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4007 /* These are all METH_NOARGS, so don't need to check the arglist. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4008 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4009 datetime_utcoffset(PyDateTime_DateTime *self, PyObject *unused) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4010 return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4011 "utcoffset", (PyObject *)self); |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
4012 } |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
4013 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
4014 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4015 datetime_dst(PyDateTime_DateTime *self, PyObject *unused) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4016 return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4017 "dst", (PyObject *)self); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4018 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4019 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4020 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4021 datetime_tzname(PyDateTime_DateTime *self, PyObject *unused) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4022 return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4023 (PyObject *)self); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4024 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4025 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4026 /* |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4027 * datetime arithmetic. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4028 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4029 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4030 /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4031 * the tzinfo state of date. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4032 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4033 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4034 add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4035 int factor) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4036 { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4037 /* Note that the C-level additions can't overflow, because of |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4038 * invariant bounds on the member values. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4039 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4040 int year = GET_YEAR(date); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4041 int month = GET_MONTH(date); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4042 int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4043 int hour = DATE_GET_HOUR(date); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4044 int minute = DATE_GET_MINUTE(date); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4045 int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4046 int microsecond = DATE_GET_MICROSECOND(date) + |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4047 GET_TD_MICROSECONDS(delta) * factor; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4048 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4049 assert(factor == 1 || factor == -1); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4050 if (normalize_datetime(&year, &month, &day, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4051 &hour, &minute, &second, µsecond) < 0) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4052 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4053 else |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4054 return new_datetime(year, month, day, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4055 hour, minute, second, microsecond, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4056 HASTZINFO(date) ? date->tzinfo : Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4057 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4058 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4059 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4060 datetime_add(PyObject *left, PyObject *right) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4061 { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4062 if (PyDateTime_Check(left)) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4063 /* datetime + ??? */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4064 if (PyDelta_Check(right)) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4065 /* datetime + delta */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4066 return add_datetime_timedelta( |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4067 (PyDateTime_DateTime *)left, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4068 (PyDateTime_Delta *)right, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4069 1); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4070 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4071 else if (PyDelta_Check(left)) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4072 /* delta + datetime */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4073 return add_datetime_timedelta((PyDateTime_DateTime *) right, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4074 (PyDateTime_Delta *) left, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4075 1); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4076 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4077 Py_INCREF(Py_NotImplemented); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4078 return Py_NotImplemented; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4079 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4080 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4081 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4082 datetime_subtract(PyObject *left, PyObject *right) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4083 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4084 PyObject *result = Py_NotImplemented; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4085 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4086 if (PyDateTime_Check(left)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4087 /* datetime - ??? */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4088 if (PyDateTime_Check(right)) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4089 /* datetime - datetime */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4090 naivety n1, n2; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4091 int offset1, offset2; |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4092 int delta_d, delta_s, delta_us; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4093 |
|
26882
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
4094 if (classify_two_utcoffsets(left, &offset1, &n1, left, |
|
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
4095 right, &offset2, &n2, |
|
d2199fc14d06
Bite the bullet on all the indirect timetz and datetimetz tzinfo methods:
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
4096 right) < 0) |
|
26808
7f81d16e2c5d
Squash recently-introduced code duplication.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
4097 return NULL; |
|
26809
858ee11714a6
And put back a little code duplication, in the form of asserts.
Tim Peters <tim.peters@gmail.com>
parents:
26808
diff
changeset
|
4098 assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4099 if (n1 != n2) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4100 PyErr_SetString(PyExc_TypeError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4101 "can't subtract offset-naive and " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4102 "offset-aware datetimes"); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4103 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4104 } |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4105 delta_d = ymd_to_ord(GET_YEAR(left), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4106 GET_MONTH(left), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4107 GET_DAY(left)) - |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4108 ymd_to_ord(GET_YEAR(right), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4109 GET_MONTH(right), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4110 GET_DAY(right)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4111 /* These can't overflow, since the values are |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4112 * normalized. At most this gives the number of |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4113 * seconds in one day. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4114 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4115 delta_s = (DATE_GET_HOUR(left) - |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4116 DATE_GET_HOUR(right)) * 3600 + |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4117 (DATE_GET_MINUTE(left) - |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4118 DATE_GET_MINUTE(right)) * 60 + |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4119 (DATE_GET_SECOND(left) - |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4120 DATE_GET_SECOND(right)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4121 delta_us = DATE_GET_MICROSECOND(left) - |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4122 DATE_GET_MICROSECOND(right); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4123 /* (left - offset1) - (right - offset2) = |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4124 * (left - right) + (offset2 - offset1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4125 */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4126 delta_s += (offset2 - offset1) * 60; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4127 result = new_delta(delta_d, delta_s, delta_us, 1); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4128 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4129 else if (PyDelta_Check(right)) { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4130 /* datetime - delta */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4131 result = add_datetime_timedelta( |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4132 (PyDateTime_DateTime *)left, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4133 (PyDateTime_Delta *)right, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4134 -1); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4135 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4136 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4137 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4138 if (result == Py_NotImplemented) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4139 Py_INCREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4140 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4141 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4142 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4143 /* Various ways to turn a datetime into a string. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4144 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4145 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4146 datetime_repr(PyDateTime_DateTime *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4147 { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4148 char buffer[1000]; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
4149 const char *type_name = Py_TYPE(self)->tp_name; |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4150 PyObject *baserepr; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4151 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4152 if (DATE_GET_MICROSECOND(self)) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4153 PyOS_snprintf(buffer, sizeof(buffer), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4154 "%s(%d, %d, %d, %d, %d, %d, %d)", |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
4155 type_name, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4156 GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4157 DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4158 DATE_GET_SECOND(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4159 DATE_GET_MICROSECOND(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4160 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4161 else if (DATE_GET_SECOND(self)) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4162 PyOS_snprintf(buffer, sizeof(buffer), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4163 "%s(%d, %d, %d, %d, %d, %d)", |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
4164 type_name, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4165 GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4166 DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4167 DATE_GET_SECOND(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4168 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4169 else { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4170 PyOS_snprintf(buffer, sizeof(buffer), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4171 "%s(%d, %d, %d, %d, %d)", |
|
37846
d64badac6cf3
C++ compiler cleanup: "typename" is a C++ keyword
Skip Montanaro <skip@pobox.com>
parents:
37723
diff
changeset
|
4172 type_name, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4173 GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4174 DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4175 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
4176 baserepr = PyString_FromString(buffer); |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4177 if (baserepr == NULL || ! HASTZINFO(self)) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4178 return baserepr; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4179 return append_keyword_tzinfo(baserepr, self->tzinfo); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4180 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4181 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4182 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4183 datetime_str(PyDateTime_DateTime *self) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4184 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4185 return PyObject_CallMethod((PyObject *)self, "isoformat", "(s)", " "); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4186 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4187 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4188 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4189 datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4190 { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4191 char sep = 'T'; |
| 36684 | 4192 static char *keywords[] = {"sep", NULL}; |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4193 char buffer[100]; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4194 char *cp; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4195 PyObject *result; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4196 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4197 if (!PyArg_ParseTupleAndKeywords(args, kw, "|c:isoformat", keywords, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4198 &sep)) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4199 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4200 cp = isoformat_date((PyDateTime_Date *)self, buffer, sizeof(buffer)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4201 assert(cp != NULL); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4202 *cp++ = sep; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4203 isoformat_time(self, cp, sizeof(buffer) - (cp - buffer)); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
4204 result = PyString_FromString(buffer); |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4205 if (result == NULL || ! HASTZINFO(self)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4206 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4207 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4208 /* We need to append the UTC offset. */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4209 if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4210 (PyObject *)self) < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4211 Py_DECREF(result); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4212 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4213 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
4214 PyString_ConcatAndDel(&result, PyString_FromString(buffer)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4215 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4216 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4217 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4218 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4219 datetime_ctime(PyDateTime_DateTime *self) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4220 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4221 return format_ctime((PyDateTime_Date *)self, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4222 DATE_GET_HOUR(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4223 DATE_GET_MINUTE(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4224 DATE_GET_SECOND(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4225 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4226 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4227 /* Miscellaneous methods. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4228 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4229 /* This is more natural as a tp_compare, but doesn't work then: for whatever |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4230 * reason, Python's try_3way_compare ignores tp_compare unless |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4231 * PyInstance_Check returns true, but these aren't old-style classes. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4232 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4233 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4234 datetime_richcompare(PyDateTime_DateTime *self, PyObject *other, int op) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4235 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4236 int diff; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4237 naivety n1, n2; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4238 int offset1, offset2; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4239 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4240 if (! PyDateTime_Check(other)) { |
|
33498
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4241 /* If other has a "timetuple" attr, that's an advertised |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4242 * hook for other classes to ask to get comparison control. |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4243 * However, date instances have a timetuple attr, and we |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4244 * don't want to allow that comparison. Because datetime |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4245 * is a subclass of date, when mixing date and datetime |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4246 * in a comparison, Python gives datetime the first shot |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4247 * (it's the more specific subtype). So we can stop that |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4248 * combination here reliably. |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4249 */ |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4250 if (PyObject_HasAttrString(other, "timetuple") && |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32393
diff
changeset
|
4251 ! PyDate_Check(other)) { |
|
27365
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27353
diff
changeset
|
4252 /* A hook for other kinds of datetime objects. */ |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27353
diff
changeset
|
4253 Py_INCREF(Py_NotImplemented); |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27353
diff
changeset
|
4254 return Py_NotImplemented; |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27353
diff
changeset
|
4255 } |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
4256 if (op == Py_EQ || op == Py_NE) { |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
4257 PyObject *result = op == Py_EQ ? Py_False : Py_True; |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
4258 Py_INCREF(result); |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
4259 return result; |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
4260 } |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4261 /* Stop this from falling back to address comparison. */ |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27682
diff
changeset
|
4262 return cmperror((PyObject *)self, other); |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4263 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4264 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4265 if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4266 (PyObject *)self, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4267 other, &offset2, &n2, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4268 other) < 0) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4269 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4270 assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4271 /* If they're both naive, or both aware and have the same offsets, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4272 * we get off cheap. Note that if they're both naive, offset1 == |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4273 * offset2 == 0 at this point. |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4274 */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4275 if (n1 == n2 && offset1 == offset2) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4276 diff = memcmp(self->data, ((PyDateTime_DateTime *)other)->data, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4277 _PyDateTime_DATETIME_DATASIZE); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4278 return diff_to_bool(diff, op); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4279 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4280 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4281 if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4282 PyDateTime_Delta *delta; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4283 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4284 assert(offset1 != offset2); /* else last "if" handled it */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4285 delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4286 other); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4287 if (delta == NULL) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4288 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4289 diff = GET_TD_DAYS(delta); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4290 if (diff == 0) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4291 diff = GET_TD_SECONDS(delta) | |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4292 GET_TD_MICROSECONDS(delta); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4293 Py_DECREF(delta); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4294 return diff_to_bool(diff, op); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4295 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4296 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4297 assert(n1 != n2); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4298 PyErr_SetString(PyExc_TypeError, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4299 "can't compare offset-naive and " |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4300 "offset-aware datetimes"); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4301 return NULL; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4302 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4303 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4304 static long |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4305 datetime_hash(PyDateTime_DateTime *self) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4306 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4307 if (self->hashcode == -1) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4308 naivety n; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4309 int offset; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4310 PyObject *temp; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4311 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4312 n = classify_utcoffset((PyObject *)self, (PyObject *)self, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4313 &offset); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4314 assert(n != OFFSET_UNKNOWN); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4315 if (n == OFFSET_ERROR) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4316 return -1; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4317 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4318 /* Reduce this to a hash of another object. */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4319 if (n == OFFSET_NAIVE) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
4320 temp = PyString_FromStringAndSize( |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4321 (char *)self->data, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4322 _PyDateTime_DATETIME_DATASIZE); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4323 else { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4324 int days; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4325 int seconds; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4326 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4327 assert(n == OFFSET_AWARE); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4328 assert(HASTZINFO(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4329 days = ymd_to_ord(GET_YEAR(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4330 GET_MONTH(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4331 GET_DAY(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4332 seconds = DATE_GET_HOUR(self) * 3600 + |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4333 (DATE_GET_MINUTE(self) - offset) * 60 + |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4334 DATE_GET_SECOND(self); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4335 temp = new_delta(days, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4336 seconds, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4337 DATE_GET_MICROSECOND(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4338 1); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4339 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4340 if (temp != NULL) { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4341 self->hashcode = PyObject_Hash(temp); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4342 Py_DECREF(temp); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4343 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4344 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4345 return self->hashcode; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4346 } |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4347 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4348 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4349 datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4350 { |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4351 PyObject *clone; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4352 PyObject *tuple; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4353 int y = GET_YEAR(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4354 int m = GET_MONTH(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4355 int d = GET_DAY(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4356 int hh = DATE_GET_HOUR(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4357 int mm = DATE_GET_MINUTE(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4358 int ss = DATE_GET_SECOND(self); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4359 int us = DATE_GET_MICROSECOND(self); |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4360 PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4361 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4362 if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4363 datetime_kws, |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4364 &y, &m, &d, &hh, &mm, &ss, &us, |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4365 &tzinfo)) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4366 return NULL; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4367 tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4368 if (tuple == NULL) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4369 return NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
4370 clone = datetime_new(Py_TYPE(self), tuple, NULL); |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4371 Py_DECREF(tuple); |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4372 return clone; |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4373 } |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4374 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26751
diff
changeset
|
4375 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4376 datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4377 { |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4378 int y, m, d, hh, mm, ss, us; |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26882
diff
changeset
|
4379 PyObject *result; |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4380 int offset, none; |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26882
diff
changeset
|
4381 |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4382 PyObject *tzinfo; |
| 36684 | 4383 static char *keywords[] = {"tz", NULL}; |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4384 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4385 if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:astimezone", keywords, |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4386 &PyDateTime_TZInfoType, &tzinfo)) |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4387 return NULL; |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4388 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4389 if (!HASTZINFO(self) || self->tzinfo == Py_None) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4390 goto NeedAware; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4391 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4392 /* Conversion to self's own time zone is a NOP. */ |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4393 if (self->tzinfo == tzinfo) { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4394 Py_INCREF(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4395 return (PyObject *)self; |
|
27034
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27032
diff
changeset
|
4396 } |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4397 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4398 /* Convert self to UTC. */ |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4399 offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4400 if (offset == -1 && PyErr_Occurred()) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4401 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4402 if (none) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4403 goto NeedAware; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4404 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4405 y = GET_YEAR(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4406 m = GET_MONTH(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4407 d = GET_DAY(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4408 hh = DATE_GET_HOUR(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4409 mm = DATE_GET_MINUTE(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4410 ss = DATE_GET_SECOND(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4411 us = DATE_GET_MICROSECOND(self); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4412 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4413 mm -= offset; |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4414 if ((mm < 0 || mm >= 60) && |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4415 normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4416 return NULL; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4417 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4418 /* Attach new tzinfo and let fromutc() do the rest. */ |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4419 result = new_datetime(y, m, d, hh, mm, ss, us, tzinfo); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4420 if (result != NULL) { |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4421 PyObject *temp = result; |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4422 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4423 result = PyObject_CallMethod(tzinfo, "fromutc", "O", temp); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4424 Py_DECREF(temp); |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4425 } |
|
27082
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27073
diff
changeset
|
4426 return result; |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26882
diff
changeset
|
4427 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4428 NeedAware: |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4429 PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
4430 "a naive datetime"); |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26882
diff
changeset
|
4431 return NULL; |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4432 } |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4433 |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4434 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4435 datetime_timetuple(PyDateTime_DateTime *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4436 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4437 int dstflag = -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4438 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4439 if (HASTZINFO(self) && self->tzinfo != Py_None) { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4440 int none; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4441 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4442 dstflag = call_dst(self->tzinfo, (PyObject *)self, &none); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4443 if (dstflag == -1 && PyErr_Occurred()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4444 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4445 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4446 if (none) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4447 dstflag = -1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4448 else if (dstflag != 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4449 dstflag = 1; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4450 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4451 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4452 return build_struct_time(GET_YEAR(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4453 GET_MONTH(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4454 GET_DAY(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4455 DATE_GET_HOUR(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4456 DATE_GET_MINUTE(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4457 DATE_GET_SECOND(self), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4458 dstflag); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4459 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4460 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4461 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4462 datetime_getdate(PyDateTime_DateTime *self) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4463 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4464 return new_date(GET_YEAR(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4465 GET_MONTH(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4466 GET_DAY(self)); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4467 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4468 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4469 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4470 datetime_gettime(PyDateTime_DateTime *self) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4471 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4472 return new_time(DATE_GET_HOUR(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4473 DATE_GET_MINUTE(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4474 DATE_GET_SECOND(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4475 DATE_GET_MICROSECOND(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4476 Py_None); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4477 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4478 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4479 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4480 datetime_gettimetz(PyDateTime_DateTime *self) |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4481 { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4482 return new_time(DATE_GET_HOUR(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4483 DATE_GET_MINUTE(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4484 DATE_GET_SECOND(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4485 DATE_GET_MICROSECOND(self), |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4486 HASTZINFO(self) ? self->tzinfo : Py_None); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4487 } |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4488 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4489 static PyObject * |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4490 datetime_utctimetuple(PyDateTime_DateTime *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4491 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4492 int y = GET_YEAR(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4493 int m = GET_MONTH(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4494 int d = GET_DAY(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4495 int hh = DATE_GET_HOUR(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4496 int mm = DATE_GET_MINUTE(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4497 int ss = DATE_GET_SECOND(self); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4498 int us = 0; /* microseconds are ignored in a timetuple */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4499 int offset = 0; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4500 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4501 if (HASTZINFO(self) && self->tzinfo != Py_None) { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4502 int none; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4503 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4504 offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4505 if (offset == -1 && PyErr_Occurred()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4506 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4507 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4508 /* Even if offset is 0, don't call timetuple() -- tm_isdst should be |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4509 * 0 in a UTC timetuple regardless of what dst() says. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4510 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4511 if (offset) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4512 /* Subtract offset minutes & normalize. */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4513 int stat; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4514 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4515 mm -= offset; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4516 stat = normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4517 if (stat < 0) { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4518 /* At the edges, it's possible we overflowed |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4519 * beyond MINYEAR or MAXYEAR. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4520 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4521 if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4522 PyErr_Clear(); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4523 else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4524 return NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4525 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4526 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4527 return build_struct_time(y, m, d, hh, mm, ss, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4528 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4529 |
|
27572
f235f135f3f8
All over: changed comments to reflect pickling is straightforward now,
Tim Peters <tim.peters@gmail.com>
parents:
27571
diff
changeset
|
4530 /* Pickle support, a simple use of __reduce__. */ |
|
27193
934732787419
Removed more now-pointless pickle code.
Tim Peters <tim.peters@gmail.com>
parents:
27172
diff
changeset
|
4531 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4532 /* Let basestate be the non-tzinfo data string. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4533 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4534 * So it's a tuple in any (non-error) case. |
|
27575
3b9fb57fac34
There's no good reason for datetime objects to expose __getstate__()
Tim Peters <tim.peters@gmail.com>
parents:
27573
diff
changeset
|
4535 * __getstate__ isn't exposed. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4536 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4537 static PyObject * |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4538 datetime_getstate(PyDateTime_DateTime *self) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4539 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4540 PyObject *basestate; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4541 PyObject *result = NULL; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4542 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47440
diff
changeset
|
4543 basestate = PyString_FromStringAndSize((char *)self->data, |
|
27193
934732787419
Removed more now-pointless pickle code.
Tim Peters <tim.peters@gmail.com>
parents:
27172
diff
changeset
|
4544 _PyDateTime_DATETIME_DATASIZE); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4545 if (basestate != NULL) { |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4546 if (! HASTZINFO(self) || self->tzinfo == Py_None) |
|
30683
3b59fa5d25b1
Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents:
29601
diff
changeset
|
4547 result = PyTuple_Pack(1, basestate); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4548 else |
|
30683
3b59fa5d25b1
Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents:
29601
diff
changeset
|
4549 result = PyTuple_Pack(2, basestate, self->tzinfo); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4550 Py_DECREF(basestate); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4551 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4552 return result; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4553 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4554 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4555 static PyObject * |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
4556 datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4557 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
4558 return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4559 } |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4560 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4561 static PyMethodDef datetime_methods[] = { |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
4562 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4563 /* Class methods: */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4564 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4565 {"now", (PyCFunction)datetime_now, |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
4566 METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 27352 | 4567 PyDoc_STR("[tz] -> new datetime with tz's local day and time.")}, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4568 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4569 {"utcnow", (PyCFunction)datetime_utcnow, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4570 METH_NOARGS | METH_CLASS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4571 PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4572 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4573 {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
4574 METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
4575 PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4576 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4577 {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4578 METH_VARARGS | METH_CLASS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4579 PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4580 "(like time.time()).")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4581 |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
4582 {"strptime", (PyCFunction)datetime_strptime, |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
4583 METH_VARARGS | METH_CLASS, |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
4584 PyDoc_STR("string, format -> new datetime parsed from a string " |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
4585 "(like time.strptime()).")}, |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
34191
diff
changeset
|
4586 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4587 {"combine", (PyCFunction)datetime_combine, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4588 METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4589 PyDoc_STR("date, time -> datetime with same date and time fields")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4590 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4591 /* Instance methods: */ |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
4592 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4593 {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4594 PyDoc_STR("Return date object with same year, month and day.")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4595 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4596 {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4597 PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4598 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4599 {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4600 PyDoc_STR("Return time object with same time and tzinfo.")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4601 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4602 {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4603 PyDoc_STR("Return ctime() style string.")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4604 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4605 {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4606 PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4607 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4608 {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4609 PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4610 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
4611 {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4612 PyDoc_STR("[sep] -> string in ISO 8601 format, " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4613 "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4614 "sep is used to separate the year from the time, and " |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4615 "defaults to 'T'.")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4616 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4617 {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4618 PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4619 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4620 {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4621 PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4622 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4623 {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4624 PyDoc_STR("Return self.tzinfo.dst(self).")}, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4625 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
4626 {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4627 PyDoc_STR("Return datetime with new specified fields.")}, |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4628 |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41561
diff
changeset
|
4629 {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4630 PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
4631 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
4632 {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
4633 PyDoc_STR("__reduce__() -> (cls, state)")}, |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
4634 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4635 {NULL, NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4636 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4637 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4638 static char datetime_doc[] = |
|
34191
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
4639 PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
|
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
4640 \n\ |
|
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
4641 The year, month and day arguments are required. tzinfo may be None, or an\n\ |
|
cd4dd6dd9721
Bug #1087216: datetime module documentation missing critical detail
Raymond Hettinger <python@rcn.com>
parents:
33498
diff
changeset
|
4642 instance of a tzinfo subclass. The remaining arguments may be ints or longs.\n"); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4643 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4644 static PyNumberMethods datetime_as_number = { |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4645 datetime_add, /* nb_add */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4646 datetime_subtract, /* nb_subtract */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4647 0, /* nb_multiply */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4648 0, /* nb_divide */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4649 0, /* nb_remainder */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4650 0, /* nb_divmod */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4651 0, /* nb_power */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4652 0, /* nb_negative */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4653 0, /* nb_positive */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4654 0, /* nb_absolute */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4655 0, /* nb_nonzero */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4656 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4657 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4658 statichere PyTypeObject PyDateTime_DateTimeType = { |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4659 PyObject_HEAD_INIT(NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4660 0, /* ob_size */ |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27159
diff
changeset
|
4661 "datetime.datetime", /* tp_name */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4662 sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4663 0, /* tp_itemsize */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4664 (destructor)datetime_dealloc, /* tp_dealloc */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4665 0, /* tp_print */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4666 0, /* tp_getattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4667 0, /* tp_setattr */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4668 0, /* tp_compare */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4669 (reprfunc)datetime_repr, /* tp_repr */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4670 &datetime_as_number, /* tp_as_number */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4671 0, /* tp_as_sequence */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4672 0, /* tp_as_mapping */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4673 (hashfunc)datetime_hash, /* tp_hash */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4674 0, /* tp_call */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4675 (reprfunc)datetime_str, /* tp_str */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4676 PyObject_GenericGetAttr, /* tp_getattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4677 0, /* tp_setattro */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4678 0, /* tp_as_buffer */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4679 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4680 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4681 datetime_doc, /* tp_doc */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4682 0, /* tp_traverse */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4683 0, /* tp_clear */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4684 (richcmpfunc)datetime_richcompare, /* tp_richcompare */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4685 0, /* tp_weaklistoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4686 0, /* tp_iter */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4687 0, /* tp_iternext */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4688 datetime_methods, /* tp_methods */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4689 0, /* tp_members */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4690 datetime_getset, /* tp_getset */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4691 &PyDateTime_DateType, /* tp_base */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4692 0, /* tp_dict */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4693 0, /* tp_descr_get */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4694 0, /* tp_descr_set */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4695 0, /* tp_dictoffset */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4696 0, /* tp_init */ |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29138
diff
changeset
|
4697 datetime_alloc, /* tp_alloc */ |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4698 datetime_new, /* tp_new */ |
|
29138
c310f9ddfdcb
Turns out there wasn't a need to define tp_free for any of the types here.
Tim Peters <tim.peters@gmail.com>
parents:
29136
diff
changeset
|
4699 0, /* tp_free */ |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4700 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4701 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4702 /* --------------------------------------------------------------------------- |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4703 * Module methods and initialization. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4704 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4705 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4706 static PyMethodDef module_methods[] = { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4707 {NULL, NULL} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4708 }; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4709 |
|
32393
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4710 /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4711 * datetime.h. |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4712 */ |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4713 static PyDateTime_CAPI CAPI = { |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4714 &PyDateTime_DateType, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4715 &PyDateTime_DateTimeType, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4716 &PyDateTime_TimeType, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4717 &PyDateTime_DeltaType, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4718 &PyDateTime_TZInfoType, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4719 new_date_ex, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4720 new_datetime_ex, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4721 new_time_ex, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4722 new_delta_ex, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4723 datetime_fromtimestamp, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4724 date_fromtimestamp |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4725 }; |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4726 |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4727 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4728 PyMODINIT_FUNC |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4729 initdatetime(void) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4730 { |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4731 PyObject *m; /* a module object */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4732 PyObject *d; /* its dict */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4733 PyObject *x; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4734 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4735 m = Py_InitModule3("datetime", module_methods, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4736 "Fast implementation of the datetime type."); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
4737 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
4738 return; |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4739 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4740 if (PyType_Ready(&PyDateTime_DateType) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4741 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4742 if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4743 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4744 if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4745 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4746 if (PyType_Ready(&PyDateTime_TimeType) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4747 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4748 if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4749 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4750 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4751 /* timedelta values */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4752 d = PyDateTime_DeltaType.tp_dict; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4753 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4754 x = new_delta(0, 0, 1, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4755 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4756 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4757 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4758 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4759 x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4760 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4761 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4762 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4763 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4764 x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4765 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4766 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4767 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4768 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4769 /* date values */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4770 d = PyDateTime_DateType.tp_dict; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4771 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4772 x = new_date(1, 1, 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4773 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4774 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4775 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4776 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4777 x = new_date(MAXYEAR, 12, 31); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4778 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4779 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4780 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4781 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4782 x = new_delta(1, 0, 0, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4783 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4784 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4785 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4786 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
4787 /* time values */ |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
4788 d = PyDateTime_TimeType.tp_dict; |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
4789 |
|
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
4790 x = new_time(0, 0, 0, 0, Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4791 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4792 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4793 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4794 |
|
27195
65dc0110a735
Got rid of the timetz type entirely. This was a bit trickier than I
Tim Peters <tim.peters@gmail.com>
parents:
27193
diff
changeset
|
4795 x = new_time(23, 59, 59, 999999, Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4796 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4797 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4798 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4799 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4800 x = new_delta(0, 0, 1, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4801 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4802 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4803 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4804 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4805 /* datetime values */ |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4806 d = PyDateTime_DateTimeType.tp_dict; |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4807 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4808 x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4809 if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4810 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4811 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4812 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4813 x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4814 if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4815 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4816 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4817 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4818 x = new_delta(0, 0, 1, 0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4819 if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4820 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4821 Py_DECREF(x); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4822 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4823 /* module initialization */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4824 PyModule_AddIntConstant(m, "MINYEAR", MINYEAR); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4825 PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4826 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4827 Py_INCREF(&PyDateTime_DateType); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4828 PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4829 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4830 Py_INCREF(&PyDateTime_DateTimeType); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4831 PyModule_AddObject(m, "datetime", |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4832 (PyObject *)&PyDateTime_DateTimeType); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4833 |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4834 Py_INCREF(&PyDateTime_TimeType); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4835 PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
|
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4836 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4837 Py_INCREF(&PyDateTime_DeltaType); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4838 PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4839 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4840 Py_INCREF(&PyDateTime_TZInfoType); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4841 PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4842 |
|
32393
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4843 x = PyCObject_FromVoidPtrAndDesc(&CAPI, (void*) DATETIME_API_MAGIC, |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4844 NULL); |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4845 if (x == NULL) |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4846 return; |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4847 PyModule_AddObject(m, "datetime_CAPI", x); |
|
e5df8c8faa09
SF patch 876130: add C API to datetime module, from Anthony Tuininga.
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
4848 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4849 /* A 4-year cycle has an extra leap day over what we'd get from |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4850 * pasting together 4 single years. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4851 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4852 assert(DI4Y == 4 * 365 + 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4853 assert(DI4Y == days_before_year(4+1)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4854 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4855 /* Similarly, a 400-year cycle has an extra leap day over what we'd |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4856 * get from pasting together 4 100-year cycles. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4857 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4858 assert(DI400Y == 4 * DI100Y + 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4859 assert(DI400Y == days_before_year(400+1)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4860 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4861 /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4862 * pasting together 25 4-year cycles. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4863 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4864 assert(DI100Y == 25 * DI4Y - 1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4865 assert(DI100Y == days_before_year(100+1)); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4866 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4867 us_per_us = PyInt_FromLong(1); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4868 us_per_ms = PyInt_FromLong(1000); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4869 us_per_second = PyInt_FromLong(1000000); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4870 us_per_minute = PyInt_FromLong(60000000); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4871 seconds_per_day = PyInt_FromLong(24 * 3600); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4872 if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL || |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4873 us_per_minute == NULL || seconds_per_day == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4874 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4875 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4876 /* The rest are too big for 32-bit ints, but even |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4877 * us_per_week fits in 40 bits, so doubles should be exact. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4878 */ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4879 us_per_hour = PyLong_FromDouble(3600000000.0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4880 us_per_day = PyLong_FromDouble(86400000000.0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4881 us_per_week = PyLong_FromDouble(604800000000.0); |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4882 if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4883 return; |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4884 } |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4885 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4886 /* --------------------------------------------------------------------------- |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4887 Some time zone algebra. For a datetime x, let |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4888 x.n = x stripped of its timezone -- its naive time. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4889 x.o = x.utcoffset(), and assuming that doesn't raise an exception or |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4890 return None |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4891 x.d = x.dst(), and assuming that doesn't raise an exception or |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4892 return None |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4893 x.s = x's standard offset, x.o - x.d |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4894 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4895 Now some derived rules, where k is a duration (timedelta). |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4896 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4897 1. x.o = x.s + x.d |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4898 This follows from the definition of x.s. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4899 |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4900 2. If x and y have the same tzinfo member, x.s = y.s. |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4901 This is actually a requirement, an assumption we need to make about |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4902 sane tzinfo classes. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4903 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4904 3. The naive UTC time corresponding to x is x.n - x.o. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4905 This is again a requirement for a sane tzinfo class. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4906 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4907 4. (x+k).s = x.s |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4908 This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4909 |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4910 5. (x+k).n = x.n + k |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4911 Again follows from how arithmetic is defined. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4912 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4913 Now we can explain tz.fromutc(x). Let's assume it's an interesting case |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4914 (meaning that the various tzinfo methods exist, and don't blow up or return |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4915 None when called). |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4916 |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27217
diff
changeset
|
4917 The function wants to return a datetime y with timezone tz, equivalent to x. |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4918 x is already in UTC. |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4919 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4920 By #3, we want |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4921 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4922 y.n - y.o = x.n [1] |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4923 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4924 The algorithm starts by attaching tz to x.n, and calling that y. So |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4925 x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4926 becomes true; in effect, we want to solve [2] for k: |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4927 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4928 (y+k).n - (y+k).o = x.n [2] |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4929 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4930 By #1, this is the same as |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4931 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4932 (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4933 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4934 By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4935 Substituting that into [3], |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4936 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4937 x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4938 k - (y+k).s - (y+k).d = 0; rearranging, |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4939 k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4940 k = y.s - (y+k).d |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4941 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4942 On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4943 approximate k by ignoring the (y+k).d term at first. Note that k can't be |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4944 very large, since all offset-returning methods return a duration of magnitude |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4945 less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4946 be 0, so ignoring it has no consequence then. |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4947 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4948 In any case, the new value is |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4949 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4950 z = y + y.s [4] |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4951 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4952 It's helpful to step back at look at [4] from a higher level: it's simply |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4953 mapping from UTC to tz's standard time. |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4954 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4955 At this point, if |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4956 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4957 z.n - z.o = x.n [5] |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4958 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4959 we have an equivalent time, and are almost done. The insecurity here is |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4960 at the start of daylight time. Picture US Eastern for concreteness. The wall |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4961 time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4962 sense then. The docs ask that an Eastern tzinfo class consider such a time to |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4963 be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4964 on the day DST starts. We want to return the 1:MM EST spelling because that's |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4965 the only spelling that makes sense on the local wall clock. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
4966 |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4967 In fact, if [5] holds at this point, we do have the standard-time spelling, |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4968 but that takes a bit of proof. We first prove a stronger result. What's the |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4969 difference between the LHS and RHS of [5]? Let |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4970 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4971 diff = x.n - (z.n - z.o) [6] |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4972 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4973 Now |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4974 z.n = by [4] |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4975 (y + y.s).n = by #5 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4976 y.n + y.s = since y.n = x.n |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4977 x.n + y.s = since z and y are have the same tzinfo member, |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4978 y.s = z.s by #2 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4979 x.n + z.s |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4980 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4981 Plugging that back into [6] gives |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4982 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4983 diff = |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4984 x.n - ((x.n + z.s) - z.o) = expanding |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4985 x.n - x.n - z.s + z.o = cancelling |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4986 - z.s + z.o = by #2 |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4987 z.d |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4988 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4989 So diff = z.d. |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4990 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4991 If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4992 spelling we wanted in the endcase described above. We're done. Contrarily, |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4993 if z.d = 0, then we have a UTC equivalent, and are also done. |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4994 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4995 If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4996 add to z (in effect, z is in tz's standard time, and we need to shift the |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
4997 local clock into tz's daylight time). |
|
27032
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4998 |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
4999 Let |
|
b61a66022d5a
The astimezone() correctness proof endured much pain to prove what
Tim Peters <tim.peters@gmail.com>
parents:
27029
diff
changeset
|
5000 |
|
27070
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5001 z' = z + z.d = z + diff [7] |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5002 |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5003 and we can again ask whether |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5004 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5005 z'.n - z'.o = x.n [8] |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5006 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5007 If so, we're done. If not, the tzinfo class is insane, according to the |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5008 assumptions we've made. This also requires a bit of proof. As before, let's |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5009 compute the difference between the LHS and RHS of [8] (and skipping some of |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5010 the justifications for the kinds of substitutions we've done several times |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5011 already): |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5012 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5013 diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5014 x.n - (z.n + diff - z'.o) = replacing diff via [6] |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5015 x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5016 x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5017 - z.n + z.n - z.o + z'.o = cancel z.n |
|
27070
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5018 - z.o + z'.o = #1 twice |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5019 -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5020 z'.d - z.d |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5021 |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5022 So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal, |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5023 we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5024 return z', not bothering to compute z'.d. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5025 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5026 How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5027 a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5028 would have to change the result dst() returns: we start in DST, and moving |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5029 a little further into it takes us out of DST. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5030 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5031 There isn't a sane case where this can happen. The closest it gets is at |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5032 the end of DST, where there's an hour in UTC with no spelling in a hybrid |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5033 tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5034 that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5035 UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5036 time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5037 clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5038 standard time. Since that's what the local clock *does*, we want to map both |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5039 UTC hours 5:MM and 6:MM to 1:MM Eastern. The result is ambiguous |
|
27070
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5040 in local time, but so it goes -- it's the way the local clock works. |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5041 |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5042 When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5043 so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5044 z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8] |
|
27070
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5045 (correctly) concludes that z' is not UTC-equivalent to x. |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5046 |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5047 Because we know z.d said z was in daylight time (else [5] would have held and |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5048 we would have stopped then), and we know z.d != z'.d (else [8] would have held |
|
30722
651fc8d98d99
Fix a bunch of typos in documentation, docstrings and comments.
Walter Dörwald <walter@livinglogic.de>
parents:
30683
diff
changeset
|
5049 and we would have stopped then), and there are only 2 possible values dst() can |
|
27070
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5050 return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5051 but the reasoning doesn't depend on the example -- it depends on there being |
|
e92044c0b8ec
Completed astimezone()'s correctness proof. This also proves we can get
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
5052 two possible dst() outcomes, one zero and the other non-zero). Therefore |
|
27353
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5053 z' must be in standard time, and is the spelling we want in this case. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5054 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5055 Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5056 concerned (because it takes z' as being in standard time rather than the |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5057 daylight time we intend here), but returning it gives the real-life "local |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5058 clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5059 tz. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5060 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5061 When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5062 the 1:MM standard time spelling we want. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5063 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5064 So how can this break? One of the assumptions must be violated. Two |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5065 possibilities: |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5066 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5067 1) [2] effectively says that y.s is invariant across all y belong to a given |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5068 time zone. This isn't true if, for political reasons or continental drift, |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5069 a region decides to change its base offset from UTC. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5070 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5071 2) There may be versions of "double daylight" time where the tail end of |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5072 the analysis gives up a step too early. I haven't thought about that |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5073 enough to say. |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5074 |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5075 In any case, it's clear that the default fromutc() is strong enough to handle |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5076 "almost all" time zones: so long as the standard offset is invariant, it |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5077 doesn't matter if daylight time transition points change from year to year, or |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5078 if daylight time is skipped in some years; it doesn't matter how large or |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5079 small dst() may get within its bounds; and it doesn't even matter if some |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5080 perverse time zone returns a negative dst()). So a breaking case must be |
|
27aedcf46bcc
Updated the astimezone() proof to recover from all the last week's
Tim Peters <tim.peters@gmail.com>
parents:
27352
diff
changeset
|
5081 pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26996
diff
changeset
|
5082 --------------------------------------------------------------------------- */ |
