@@ -737,6 +737,52 @@ the local timezone used by methods such as localtime, but this behaviour\n\
737737should not be relied on." );
738738#endif /* HAVE_WORKING_TZSET */
739739
740+ static PyObject *
741+ time_wallclock (PyObject * self , PyObject * unused )
742+ {
743+ #if defined(MS_WINDOWS ) && !defined(__BORLANDC__ )
744+ return time_clock (self , NULL );
745+ #elif defined(HAVE_CLOCK_GETTIME ) && defined(CLOCK_MONOTONIC )
746+ static int clk_index = 0 ;
747+ clockid_t clk_ids [] = {
748+ #ifdef CLOCK_MONOTONIC_RAW
749+ CLOCK_MONOTONIC_RAW ,
750+ #endif
751+ CLOCK_MONOTONIC
752+ #ifdef CLOCK_REALTIME
753+ /* On Linux, CLOCK_REALTIME uses the same clock than gettimeofday(),
754+ but clock_gettime() has a nanosecond resolution. */
755+ , CLOCK_REALTIME
756+ #endif
757+ };
758+ int ret ;
759+ struct timespec tp ;
760+
761+ while (0 <= clk_index ) {
762+ clockid_t clk_id = clk_ids [clk_index ];
763+ ret = clock_gettime (clk_id , & tp );
764+ if (ret == 0 )
765+ return PyFloat_FromDouble (tp .tv_sec + tp .tv_nsec * 1e-9 );
766+
767+ clk_index ++ ;
768+ if (Py_ARRAY_LENGTH (clk_ids ) <= clk_index )
769+ clk_index = -1 ;
770+ }
771+ return time_time (self , NULL );
772+ #else
773+ return time_time (self , NULL );
774+ #endif
775+ }
776+
777+ PyDoc_STRVAR (wallclock_doc ,
778+ "wallclock() -> float\n\
779+ \n\
780+ Return the current time in fractions of a second to the system's best\n\
781+ ability. Use this when the most accurate representation of wall-clock is\n\
782+ required, i.e. when \"processor time\" is inappropriate. The reference point\n\
783+ of the returned value is undefined so only the difference of consecutive\n\
784+ calls is valid." );
785+
740786static void
741787PyInit_timezone (PyObject * m ) {
742788 /* This code moved from PyInit_time wholesale to allow calling it from
@@ -872,6 +918,7 @@ static PyMethodDef time_methods[] = {
872918#ifdef HAVE_WORKING_TZSET
873919 {"tzset" , time_tzset , METH_NOARGS , tzset_doc },
874920#endif
921+ {"wallclock" , time_wallclock , METH_NOARGS , wallclock_doc },
875922 {NULL , NULL } /* sentinel */
876923};
877924
0 commit comments