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