Branch data Line data Source code
1 : : #include "parts.h"
2 : :
3 : : #ifdef MS_WINDOWS
4 : : # include <winsock2.h> // struct timeval
5 : : #endif
6 : :
7 : : static PyObject *
8 : 0 : test_pytime_fromseconds(PyObject *self, PyObject *args)
9 : : {
10 : : int seconds;
11 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "i", &seconds)) {
12 : 0 : return NULL;
13 : : }
14 : 0 : _PyTime_t ts = _PyTime_FromSeconds(seconds);
15 : 0 : return _PyTime_AsNanosecondsObject(ts);
16 : : }
17 : :
18 : : static int
19 : 0 : check_time_rounding(int round)
20 : : {
21 [ # # ]: 0 : if (round != _PyTime_ROUND_FLOOR
22 [ # # ]: 0 : && round != _PyTime_ROUND_CEILING
23 [ # # ]: 0 : && round != _PyTime_ROUND_HALF_EVEN
24 [ # # ]: 0 : && round != _PyTime_ROUND_UP)
25 : : {
26 : 0 : PyErr_SetString(PyExc_ValueError, "invalid rounding");
27 : 0 : return -1;
28 : : }
29 : 0 : return 0;
30 : : }
31 : :
32 : : static PyObject *
33 : 0 : test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
34 : : {
35 : : PyObject *obj;
36 : : int round;
37 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
38 : 0 : return NULL;
39 : : }
40 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
41 : 0 : return NULL;
42 : : }
43 : : _PyTime_t ts;
44 [ # # ]: 0 : if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
45 : 0 : return NULL;
46 : : }
47 : 0 : return _PyTime_AsNanosecondsObject(ts);
48 : : }
49 : :
50 : : static PyObject *
51 : 0 : test_pytime_assecondsdouble(PyObject *self, PyObject *args)
52 : : {
53 : : PyObject *obj;
54 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "O", &obj)) {
55 : 0 : return NULL;
56 : : }
57 : : _PyTime_t ts;
58 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
59 : 0 : return NULL;
60 : : }
61 : 0 : double d = _PyTime_AsSecondsDouble(ts);
62 : 0 : return PyFloat_FromDouble(d);
63 : : }
64 : :
65 : : static PyObject *
66 : 0 : test_PyTime_AsTimeval(PyObject *self, PyObject *args)
67 : : {
68 : : PyObject *obj;
69 : : int round;
70 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
71 : 0 : return NULL;
72 : : }
73 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
74 : 0 : return NULL;
75 : : }
76 : : _PyTime_t t;
77 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
78 : 0 : return NULL;
79 : : }
80 : : struct timeval tv;
81 [ # # ]: 0 : if (_PyTime_AsTimeval(t, &tv, round) < 0) {
82 : 0 : return NULL;
83 : : }
84 : :
85 : 0 : PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
86 [ # # ]: 0 : if (seconds == NULL) {
87 : 0 : return NULL;
88 : : }
89 : 0 : return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
90 : : }
91 : :
92 : : static PyObject *
93 : 0 : test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
94 : : {
95 : : PyObject *obj;
96 : : int round;
97 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
98 : 0 : return NULL;
99 : : }
100 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
101 : 0 : return NULL;
102 : : }
103 : : _PyTime_t t;
104 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
105 : 0 : return NULL;
106 : : }
107 : : struct timeval tv;
108 : 0 : _PyTime_AsTimeval_clamp(t, &tv, round);
109 : :
110 : 0 : PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
111 [ # # ]: 0 : if (seconds == NULL) {
112 : 0 : return NULL;
113 : : }
114 : 0 : return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
115 : : }
116 : :
117 : : #ifdef HAVE_CLOCK_GETTIME
118 : : static PyObject *
119 : 0 : test_PyTime_AsTimespec(PyObject *self, PyObject *args)
120 : : {
121 : : PyObject *obj;
122 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "O", &obj)) {
123 : 0 : return NULL;
124 : : }
125 : : _PyTime_t t;
126 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
127 : 0 : return NULL;
128 : : }
129 : : struct timespec ts;
130 [ # # ]: 0 : if (_PyTime_AsTimespec(t, &ts) == -1) {
131 : 0 : return NULL;
132 : : }
133 : 0 : return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
134 : : }
135 : :
136 : : static PyObject *
137 : 0 : test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
138 : : {
139 : : PyObject *obj;
140 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "O", &obj)) {
141 : 0 : return NULL;
142 : : }
143 : : _PyTime_t t;
144 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
145 : 0 : return NULL;
146 : : }
147 : : struct timespec ts;
148 : 0 : _PyTime_AsTimespec_clamp(t, &ts);
149 : 0 : return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
150 : : }
151 : : #endif
152 : :
153 : : static PyObject *
154 : 0 : test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
155 : : {
156 : : PyObject *obj;
157 : : int round;
158 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
159 : 0 : return NULL;
160 : : }
161 : : _PyTime_t t;
162 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
163 : 0 : return NULL;
164 : : }
165 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
166 : 0 : return NULL;
167 : : }
168 : 0 : _PyTime_t ms = _PyTime_AsMilliseconds(t, round);
169 : 0 : _PyTime_t ns = _PyTime_FromNanoseconds(ms);
170 : 0 : return _PyTime_AsNanosecondsObject(ns);
171 : : }
172 : :
173 : : static PyObject *
174 : 0 : test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
175 : : {
176 : : PyObject *obj;
177 : : int round;
178 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
179 : 0 : return NULL;
180 : : }
181 : : _PyTime_t t;
182 [ # # ]: 0 : if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
183 : 0 : return NULL;
184 : : }
185 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
186 : 0 : return NULL;
187 : : }
188 : 0 : _PyTime_t us = _PyTime_AsMicroseconds(t, round);
189 : 0 : _PyTime_t ns = _PyTime_FromNanoseconds(us);
190 : 0 : return _PyTime_AsNanosecondsObject(ns);
191 : : }
192 : :
193 : : static PyObject *
194 : 0 : test_pytime_object_to_time_t(PyObject *self, PyObject *args)
195 : : {
196 : : PyObject *obj;
197 : : time_t sec;
198 : : int round;
199 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) {
200 : 0 : return NULL;
201 : : }
202 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
203 : 0 : return NULL;
204 : : }
205 [ # # ]: 0 : if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) {
206 : 0 : return NULL;
207 : : }
208 : 0 : return _PyLong_FromTime_t(sec);
209 : : }
210 : :
211 : : static PyObject *
212 : 0 : test_pytime_object_to_timeval(PyObject *self, PyObject *args)
213 : : {
214 : : PyObject *obj;
215 : : time_t sec;
216 : : long usec;
217 : : int round;
218 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) {
219 : 0 : return NULL;
220 : : }
221 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
222 : 0 : return NULL;
223 : : }
224 [ # # ]: 0 : if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) {
225 : 0 : return NULL;
226 : : }
227 : 0 : return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
228 : : }
229 : :
230 : : static PyObject *
231 : 0 : test_pytime_object_to_timespec(PyObject *self, PyObject *args)
232 : : {
233 : : PyObject *obj;
234 : : time_t sec;
235 : : long nsec;
236 : : int round;
237 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) {
238 : 0 : return NULL;
239 : : }
240 [ # # ]: 0 : if (check_time_rounding(round) < 0) {
241 : 0 : return NULL;
242 : : }
243 [ # # ]: 0 : if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) {
244 : 0 : return NULL;
245 : : }
246 : 0 : return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
247 : : }
248 : :
249 : : static PyMethodDef test_methods[] = {
250 : : {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
251 : : {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
252 : : {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
253 : : #ifdef HAVE_CLOCK_GETTIME
254 : : {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
255 : : {"PyTime_AsTimespec_clamp", test_PyTime_AsTimespec_clamp, METH_VARARGS},
256 : : #endif
257 : : {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
258 : : {"PyTime_AsTimeval_clamp", test_PyTime_AsTimeval_clamp, METH_VARARGS},
259 : : {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
260 : : {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
261 : : {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
262 : : {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
263 : : {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
264 : : {NULL},
265 : : };
266 : :
267 : : int
268 : 2 : _PyTestCapi_Init_PyTime(PyObject *mod)
269 : : {
270 [ - + ]: 2 : if (PyModule_AddFunctions(mod, test_methods) < 0) {
271 : 0 : return -1;
272 : : }
273 : 2 : return 0;
274 : : }
|