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