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