@@ -135,6 +135,54 @@ the first call to clock(). This has as much precision as the system\n\
135135records." );
136136#endif
137137
138+ #ifdef HAVE_CLOCK_GETTIME
139+ static PyObject *
140+ time_clock_gettime (PyObject * self , PyObject * args )
141+ {
142+ int ret ;
143+ clockid_t clk_id ;
144+ struct timespec tp ;
145+
146+ if (!PyArg_ParseTuple (args , "i:clock_gettime" , & clk_id ))
147+ return NULL ;
148+
149+ ret = clock_gettime ((clockid_t )clk_id , & tp );
150+ if (ret != 0 )
151+ PyErr_SetFromErrno (PyExc_IOError );
152+
153+ return PyFloat_FromDouble (tp .tv_sec + tp .tv_nsec * 1e-9 );
154+ }
155+
156+ PyDoc_STRVAR (clock_gettime_doc ,
157+ "clock_gettime(clk_id) -> floating point number\n\
158+ \n\
159+ Return the time of the specified clock clk_id." );
160+ #endif
161+
162+ #ifdef HAVE_CLOCK_GETRES
163+ static PyObject *
164+ time_clock_getres (PyObject * self , PyObject * args )
165+ {
166+ int ret ;
167+ clockid_t clk_id ;
168+ struct timespec tp ;
169+
170+ if (!PyArg_ParseTuple (args , "i:clock_getres" , & clk_id ))
171+ return NULL ;
172+
173+ ret = clock_getres ((clockid_t )clk_id , & tp );
174+ if (ret != 0 )
175+ PyErr_SetFromErrno (PyExc_IOError );
176+
177+ return PyFloat_FromDouble (tp .tv_sec + tp .tv_nsec * 1e-9 );
178+ }
179+
180+ PyDoc_STRVAR (clock_getres_doc ,
181+ "clock_getres(clk_id) -> floating point number\n\
182+ \n\
183+ Return the resolution (precision) of the specified clock clk_id." );
184+ #endif
185+
138186static PyObject *
139187time_sleep (PyObject * self , PyObject * args )
140188{
@@ -786,13 +834,37 @@ PyInit_timezone(PyObject *m) {
786834 Py_BuildValue ("(zz)" , _tzname [0 ], _tzname [1 ]));
787835#endif /* __CYGWIN__ */
788836#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
837+
838+ #if defined(HAVE_CLOCK_GETTIME ) || defined(HAVE_CLOCK_GETRES )
839+ #ifdef CLOCK_REALTIME
840+ PyModule_AddIntMacro (m , CLOCK_REALTIME );
841+ #endif
842+ #ifdef CLOCK_MONOTONIC
843+ PyModule_AddIntMacro (m , CLOCK_MONOTONIC );
844+ #endif
845+ #ifdef CLOCK_MONOTONIC_RAW
846+ PyModule_AddIntMacro (m , CLOCK_MONOTONIC_RAW );
847+ #endif
848+ #ifdef CLOCK_PROCESS_CPUTIME_ID
849+ PyModule_AddIntMacro (m , CLOCK_PROCESS_CPUTIME_ID );
850+ #endif
851+ #ifdef CLOCK_THREAD_CPUTIME_ID
852+ PyModule_AddIntMacro (m , CLOCK_THREAD_CPUTIME_ID );
853+ #endif
854+ #endif /* HAVE_CLOCK_GETTIME */
789855}
790856
791857
792858static PyMethodDef time_methods [] = {
793859 {"time" , time_time , METH_NOARGS , time_doc },
794860#if (defined (MS_WINDOWS ) && !defined (__BORLANDC__ )) || defined (HAVE_CLOCK )
795861 {"clock" , time_clock , METH_NOARGS , clock_doc },
862+ #endif
863+ #ifdef HAVE_CLOCK_GETTIME
864+ {"clock_gettime" , time_clock_gettime , METH_VARARGS , clock_gettime_doc },
865+ #endif
866+ #ifdef HAVE_CLOCK_GETRES
867+ {"clock_getres" , time_clock_getres , METH_VARARGS , clock_getres_doc },
796868#endif
797869 {"sleep" , time_sleep , METH_VARARGS , sleep_doc },
798870 {"gmtime" , time_gmtime , METH_VARARGS , gmtime_doc },
0 commit comments