1+ # Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+ # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+ #
4+ # The Universal Permissive License (UPL), Version 1.0
5+ #
6+ # Subject to the condition set forth below, permission is hereby granted to any
7+ # person obtaining a copy of this software, associated documentation and/or
8+ # data (collectively the "Software"), free of charge and under any and all
9+ # copyright rights in the Software, and any and all patent rights owned or
10+ # freely licensable by each licensor hereunder covering either (i) the
11+ # unmodified Software as contributed to or provided by such licensor, or (ii)
12+ # the Larger Works (as defined below), to deal in both
13+ #
14+ # (a) the Software, and
15+ #
16+ # (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+ # one is included with the Software each a "Larger Work" to which the Software
18+ # is contributed by such licensors),
19+ #
20+ # without restriction, including without limitation the rights to copy, create
21+ # derivative works of, display, perform, and distribute the Software and make,
22+ # use, sell, offer for sale, import, export, have made, and have sold the
23+ # Software and the Larger Work(s), and to sublicense the foregoing rights on
24+ # either these or other terms.
25+ #
26+ # This license is subject to the following condition:
27+ #
28+ # The above copyright notice and either this complete permission notice or at a
29+ # minimum a reference to the UPL must be included in all copies or substantial
30+ # portions of the Software.
31+ #
32+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+ # SOFTWARE.
39+
40+ import datetime
41+
42+ from . import CPyExtType
43+
44+ __dir__ = __file__ .rpartition ("/" )[0 ]
45+
46+
47+ class TestDateTime (object ):
48+
49+ def test_date_type (self ):
50+ TestDate = CPyExtType ("TestDate" ,
51+ """
52+ PyTypeObject* getDateType() {
53+ PyDateTime_IMPORT;
54+ PyTypeObject* t = PyDateTimeAPI->DateType;
55+ Py_XINCREF(t);
56+ return t;
57+ }
58+ """ ,
59+ tp_methods = '{"getDateType", (PyCFunction)getDateType, METH_NOARGS, ""}' ,
60+ includes = '#include "datetime.h"' ,
61+ )
62+ tester = TestDate ()
63+ assert tester .getDateType () == datetime .date
64+
65+ def test_datetime_type (self ):
66+ TestDateTime = CPyExtType ("TestDateTime" ,
67+ """
68+ PyTypeObject* getDateTimeType() {
69+ PyDateTime_IMPORT;
70+ PyTypeObject* t = PyDateTimeAPI->DateTimeType;
71+ Py_XINCREF(t);
72+ return t;
73+ }
74+ """ ,
75+ tp_methods = '{"getDateTimeType", (PyCFunction)getDateTimeType, METH_NOARGS, ""}' ,
76+ includes = '#include "datetime.h"' ,
77+ )
78+ tester = TestDateTime ()
79+ assert tester .getDateTimeType () == datetime .datetime
80+
81+ def test_time_type (self ):
82+ TestTime = CPyExtType ("TestTime" ,
83+ """
84+ PyTypeObject* getTimeType() {
85+ PyDateTime_IMPORT;
86+ PyTypeObject* t = PyDateTimeAPI->TimeType;
87+ Py_XINCREF(t);
88+ return t;
89+ }
90+ """ ,
91+ tp_methods = '{"getTimeType", (PyCFunction)getTimeType, METH_NOARGS, ""}' ,
92+ includes = '#include "datetime.h"' ,
93+ )
94+ tester = TestTime ()
95+ assert tester .getTimeType () == datetime .time
96+
97+ def test_timedelta_type (self ):
98+ TestTimeDelta = CPyExtType ("TestTimeDelta" ,
99+ """
100+ PyTypeObject* getTimeDeltaType() {
101+ PyDateTime_IMPORT;
102+ PyTypeObject* t = PyDateTimeAPI->DeltaType;
103+ Py_XINCREF(t);
104+ return t;
105+ }
106+ """ ,
107+ tp_methods = '{"getTimeDeltaType", (PyCFunction)getTimeDeltaType, METH_NOARGS, ""}' ,
108+ includes = '#include "datetime.h"' ,
109+ )
110+ tester = TestTimeDelta ()
111+ assert tester .getTimeDeltaType () == datetime .timedelta
112+
113+ def test_tzinfo_type (self ):
114+ TestTZInfo = CPyExtType ("TestTZInfo" ,
115+ """
116+ PyTypeObject* getTZInfoType() {
117+ PyDateTime_IMPORT;
118+ PyTypeObject* t = PyDateTimeAPI->TZInfoType;
119+ Py_XINCREF(t);
120+ return t;
121+ }
122+ """ ,
123+ tp_methods = '{"getTZInfoType", (PyCFunction)getTZInfoType, METH_NOARGS, ""}' ,
124+ includes = '#include "datetime.h"' ,
125+ )
126+ tester = TestTZInfo ()
127+ assert tester .getTZInfoType () == datetime .tzinfo
128+
129+ def test_timezone (self ):
130+ TestTimezone = CPyExtType ("TestTimezone" ,
131+ """
132+ PyObject* getTimezone() {
133+ PyDateTime_IMPORT;
134+ PyObject* t = PyDateTimeAPI->TimeZone_UTC;
135+ Py_XINCREF(t);
136+ return t;
137+ }
138+ """ ,
139+ tp_methods = '{"getTimezone", (PyCFunction)getTimezone, METH_NOARGS, ""}' ,
140+ includes = '#include "datetime.h"' ,
141+ )
142+ tester = TestTimezone ()
143+ assert tester .getTimezone () == datetime .timezone .utc
144+
145+
146+ def test_date_from_date (self ):
147+ TestDateFromDate = CPyExtType ("TestDateFromDate" ,
148+ """
149+ PyObject* getDate() {
150+ PyDateTime_IMPORT;
151+ PyObject* o = PyDateTimeAPI->Date_FromDate(1, 1, 1, PyDateTimeAPI->DateType);
152+ Py_XINCREF(o);
153+ return o;
154+ }
155+ """ ,
156+ tp_methods = '{"getDate", (PyCFunction)getDate, METH_NOARGS, ""}' ,
157+ includes = '#include "datetime.h"' ,
158+ )
159+ tester = TestDateFromDate ()
160+ assert tester .getDate () == datetime .date (1 , 1 , 1 )
161+
162+ def test_datetime_from_date_and_time (self ):
163+ TestDateTimeFromDateAndTime = CPyExtType ("TestDateTimeFromDateAndTime" ,
164+ """
165+ PyObject* getDateTime() {
166+ PyDateTime_IMPORT;
167+ PyObject* o = PyDateTimeAPI->DateTime_FromDateAndTime(1, 1, 1, 1, 1, 1, 1, Py_None, PyDateTimeAPI->DateTimeType);
168+ Py_XINCREF(o);
169+ return o;
170+ }
171+ """ ,
172+ tp_methods = '{"getDateTime", (PyCFunction)getDateTime, METH_NOARGS, ""}' ,
173+ includes = '#include "datetime.h"' ,
174+ )
175+ tester = TestDateTimeFromDateAndTime ()
176+ assert tester .getDateTime () == datetime .datetime (1 , 1 , 1 , 1 , 1 , 1 , 1 )
177+
178+ def test_time_from_time (self ):
179+ TestTimeFromTime = CPyExtType ("TestTimeFromTime" ,
180+ """
181+ PyObject* getTime() {
182+ PyDateTime_IMPORT;
183+ PyObject* o = PyDateTimeAPI->Time_FromTime(1, 1, 1, 1, Py_None, PyDateTimeAPI->TimeType);
184+ Py_XINCREF(o);
185+ return o;
186+ }
187+ """ ,
188+ tp_methods = '{"getTime", (PyCFunction)getTime, METH_NOARGS, ""}' ,
189+ includes = '#include "datetime.h"' ,
190+ )
191+ tester = TestTimeFromTime ()
192+ tester .getTime () == datetime .time (1 , 1 , 1 , 1 )
193+
194+ def test_delta_from_delta (self ):
195+ TestDeltaFromDelta = CPyExtType ("TestDeltaFromDelta" ,
196+ """
197+ PyObject* getDelta() {
198+ PyDateTime_IMPORT;
199+ PyObject* o = PyDateTimeAPI->Delta_FromDelta(1, 1, 1, 1, PyDateTimeAPI->DeltaType);
200+ Py_XINCREF(o);
201+ return o;
202+ }
203+ """ ,
204+ tp_methods = '{"getDelta", (PyCFunction)getDelta, METH_NOARGS, ""}' ,
205+ includes = '#include "datetime.h"' ,
206+ )
207+ tester = TestDeltaFromDelta ()
208+
209+ assert tester .getDelta () == datetime .timedelta (1 , 1 , 1 )
210+
211+ def test_timezone_from_timezone (self ):
212+ TestTimezoneFromTimezone = CPyExtType ("TestTimezoneFromTimezone" ,
213+ """
214+ PyObject* getTZ() {
215+ PyDateTime_IMPORT;
216+ PyObject* d = PyDateTimeAPI->Delta_FromDelta(0, 0, 1, 0, PyDateTimeAPI->DeltaType);
217+ Py_XINCREF(d);
218+ PyObject* o = PyDateTimeAPI->TimeZone_FromTimeZone(d, PyUnicode_FromString("CET"));
219+ Py_XINCREF(o);
220+ return o;
221+ }
222+ """ ,
223+ tp_methods = '{"getTZ", (PyCFunction)getTZ, METH_NOARGS, ""}' ,
224+ includes = '#include "datetime.h"' ,
225+ )
226+ tester = TestTimezoneFromTimezone ()
227+ assert tester .getTZ () == datetime .timezone (datetime .timedelta (0 ,0 ,1 ,0 ), "CET" )
228+
229+ def test_time_from_time_and_fold (self ):
230+ TestTimeFromTimeAndFold = CPyExtType ("TestTimeFromTimeAndFold" ,
231+ """
232+ PyObject* getTime() {
233+ PyDateTime_IMPORT;
234+ PyObject* o = PyDateTimeAPI->Time_FromTimeAndFold(1, 1, 1, 1, Py_None, 1, PyDateTimeAPI->TimeType);
235+ Py_XINCREF(o);
236+ return o;
237+ }
238+ """ ,
239+ tp_methods = '{"getTime", (PyCFunction)getTime, METH_NOARGS, ""}' ,
240+ includes = '#include "datetime.h"' ,
241+ )
242+ tester = TestTimeFromTimeAndFold ()
243+ assert tester .getTime () == datetime .time (1 , 1 , 1 , 1 , None , fold = True )
244+
245+ def test_datetime_from_date_and_time_and_fold (self ):
246+ TestDateTimeFromDateAndTimeAndFold = CPyExtType ("TestDateTimeFromDateAndTimeAndFold" ,
247+ """
248+ PyObject* getDateTime() {
249+ PyDateTime_IMPORT;
250+ PyObject* o = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(1, 1, 1, 1, 1, 1, 1, Py_None, 1, PyDateTimeAPI->DateTimeType);
251+ Py_XINCREF(o);
252+ return o;
253+ }
254+ """ ,
255+ tp_methods = '{"getDateTime", (PyCFunction)getDateTime, METH_NOARGS, ""}' ,
256+ includes = '#include "datetime.h"' ,
257+ )
258+ tester = TestDateTimeFromDateAndTimeAndFold ()
259+ assert tester .getDateTime () == datetime .datetime (1 , 1 , 1 , 1 , 1 , 1 , 1 , None , fold = True )
260+
261+ def test_date_from_timestamp (self ):
262+ TestDateFromTimestamp = CPyExtType ("TestDateFromTimestamp" ,
263+ """
264+ PyObject* getDate(PyObject *self, PyObject *args) {
265+ PyDateTime_IMPORT;
266+ PyObject* o = PyDateTimeAPI->Date_FromTimestamp((PyObject *)PyDateTimeAPI->DateType, args);
267+ Py_XINCREF(o);
268+ return o;
269+ }
270+ """ ,
271+ tp_methods = '{"getDate", (PyCFunction)getDate, METH_VARARGS, ""}' ,
272+ includes = '#include "datetime.h"' ,
273+ )
274+ tester = TestDateFromTimestamp ()
275+ ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
276+ assert tester .getDate (int (ts )) == datetime .date .fromtimestamp (int (ts ))
277+
278+ def test_datetime_from_timestamp (self ):
279+ TestDatetimeFromTimestamp = CPyExtType ("TestDatetimeFromTimestamp" ,
280+ """
281+ PyObject* getDatetime(PyObject *self, PyObject *args, PyObject *kwds) {
282+ PyDateTime_IMPORT;
283+ PyObject* o = PyDateTimeAPI->DateTime_FromTimestamp((PyObject *)PyDateTimeAPI->DateTimeType, args, kwds);
284+ Py_XINCREF(o);
285+ return o;
286+ }
287+ """ ,
288+ tp_methods = '{"getDatetime", (PyCFunction)getDatetime, METH_VARARGS | METH_KEYWORDS, ""}' ,
289+ includes = '#include "datetime.h"' ,
290+ )
291+ tester = TestDatetimeFromTimestamp ()
292+ ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
293+ assert tester .getDatetime (int (ts )) == datetime .datetime .fromtimestamp (int (ts ))
294+
295+ def test_datetime_from_timestamp_and_tz (self ):
296+ TestDatetimeFromTimestamp = CPyExtType ("TestDatetimeFromTimestamp" ,
297+ """
298+ PyObject* getDatetime(PyObject *self, PyObject *args, PyObject *kwds) {
299+ PyDateTime_IMPORT;
300+ PyObject* o = PyDateTimeAPI->DateTime_FromTimestamp((PyObject *)PyDateTimeAPI->DateTimeType, args, kwds);
301+ Py_XINCREF(o);
302+ return o;
303+ }
304+ """ ,
305+ tp_methods = '{"getDatetime", (PyCFunction)getDatetime, METH_VARARGS | METH_KEYWORDS, ""}' ,
306+ includes = '#include "datetime.h"' ,
307+ )
308+ tester = TestDatetimeFromTimestamp ()
309+ ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
310+ tz = datetime .timezone (datetime .timedelta (hours = 3 ))
311+ assert tester .getDatetime (int (ts ), tz ) == datetime .datetime .fromtimestamp (int (ts ), tz )
312+
313+ def test_datetime_from_timestamp_and_tz_kwd (self ):
314+ TestDatetimeFromTimestamp = CPyExtType ("TestDatetimeFromTimestamp" ,
315+ """
316+ PyObject* getDatetime(PyObject *self, PyObject *args, PyObject *kwds) {
317+ PyDateTime_IMPORT;
318+ PyObject* o = PyDateTimeAPI->DateTime_FromTimestamp((PyObject *)PyDateTimeAPI->DateTimeType, args, kwds);
319+ Py_XINCREF(o);
320+ return o;
321+ }
322+ """ ,
323+ tp_methods = '{"getDatetime", (PyCFunction)getDatetime, METH_VARARGS | METH_KEYWORDS, ""}' ,
324+ includes = '#include "datetime.h"' ,
325+ )
326+ tester = TestDatetimeFromTimestamp ()
327+ ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
328+ tz = datetime .timezone (datetime .timedelta (hours = 3 ))
329+ assert tester .getDatetime (int (ts ), tz = tz ) == datetime .datetime .fromtimestamp (int (ts ), tz = tz )
330+
331+ def test_write_and_invoke_member (self ):
332+ TestWriteAndInvokeMemeber = CPyExtType ("TestWriteAndInvokeMemeber" ,
333+ """
334+ PyObject* anotherDTFromDT(int y, int m, int d, PyTypeObject* t) {
335+ return PyUnicode_FromString("foo");
336+ }
337+
338+ PyObject* getDate() {
339+ PyDateTime_IMPORT;
340+ PyObject *(*temp)(int, int, int, PyTypeObject*) = PyDateTimeAPI->Date_FromDate;
341+ PyDateTimeAPI->Date_FromDate = anotherDTFromDT;
342+ PyObject* r = PyDateTimeAPI->Date_FromDate(42, 1, 1, PyDateTimeAPI->DateType);
343+ PyDateTimeAPI->Date_FromDate = temp;
344+ Py_XINCREF(r);
345+ return r;
346+ }
347+ """ ,
348+ tp_methods = '{"getDate", (PyCFunction)getDate, METH_NOARGS, ""}' ,
349+ includes = '#include "datetime.h"' ,
350+ )
351+ tester = TestWriteAndInvokeMemeber ()
352+ assert tester .getDate () == "foo"
0 commit comments