33#include <windows.h>
44#endif
55
6- #if defined(__APPLE__ ) && defined(HAVE_GETTIMEOFDAY ) && defined(HAVE_FTIME )
7- /*
8- * _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter
9- * might fail on some platforms. This fallback is unwanted on MacOSX because
10- * that makes it impossible to use a binary build on OSX 10.4 on earlier
11- * releases of the OS. Therefore claim we don't support ftime.
12- */
13- # undef HAVE_FTIME
14- #endif
15-
16- #if defined(HAVE_FTIME ) && !defined(MS_WINDOWS )
17- #include <sys/timeb.h>
18- extern int ftime (struct timeb * );
19- #endif
20-
21- static void
22- pygettimeofday (_PyTime_timeval * tp , _Py_clock_info_t * info )
6+ static int
7+ pygettimeofday (_PyTime_timeval * tp , _Py_clock_info_t * info , int raise )
238{
249#ifdef MS_WINDOWS
2510 FILETIME system_time ;
2611 ULARGE_INTEGER large ;
2712 ULONGLONG microseconds ;
2813
14+ assert (info == NULL || raise );
15+
2916 GetSystemTimeAsFileTime (& system_time );
3017 large .u .LowPart = system_time .dwLowDateTime ;
3118 large .u .HighPart = system_time .dwHighDateTime ;
@@ -37,55 +24,51 @@ pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info)
3724 tp -> tv_usec = microseconds % 1000000 ;
3825 if (info ) {
3926 DWORD timeAdjustment , timeIncrement ;
40- BOOL isTimeAdjustmentDisabled ;
27+ BOOL isTimeAdjustmentDisabled , ok ;
4128
4229 info -> implementation = "GetSystemTimeAsFileTime()" ;
4330 info -> monotonic = 0 ;
44- (void ) GetSystemTimeAdjustment (& timeAdjustment , & timeIncrement ,
45- & isTimeAdjustmentDisabled );
31+ ok = GetSystemTimeAdjustment (& timeAdjustment , & timeIncrement ,
32+ & isTimeAdjustmentDisabled );
33+ if (!ok ) {
34+ PyErr_SetFromWindowsErr (0 );
35+ return -1 ;
36+ }
4637 info -> resolution = timeIncrement * 1e-7 ;
4738 info -> adjustable = 1 ;
4839 }
49- #else
50- /* There are three ways to get the time:
51- (1) gettimeofday() -- resolution in microseconds
52- (2) ftime() -- resolution in milliseconds
53- (3) time() -- resolution in seconds
54- In all cases the return value in a timeval struct.
55- Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
56- fail, so we fall back on ftime() or time().
57- Note: clock resolution does not imply clock accuracy! */
40+ return 0 ;
5841
59- #if (defined(HAVE_CLOCK_GETTIME ) || defined(HAVE_GETTIMEOFDAY ) \
60- || defined(HAVE_FTIME ))
42+ #else /* MS_WINDOWS */
6143 int err ;
62- #endif
6344#ifdef HAVE_CLOCK_GETTIME
6445 struct timespec ts ;
6546#endif
66- #ifdef HAVE_FTIME
67- struct timeb t ;
68- #endif
6947
70- /* test clock_gettime(CLOCK_REALTIME) */
48+ assert (info == NULL || raise );
49+
7150#ifdef HAVE_CLOCK_GETTIME
72- err = clock_gettime (CLOCK_REALTIME , & ts );
73- if (err == 0 ) {
74- if (info ) {
75- struct timespec res ;
76- info -> implementation = "clock_gettime(CLOCK_REALTIME)" ;
77- info -> monotonic = 0 ;
78- info -> adjustable = 1 ;
79- if (clock_getres (CLOCK_REALTIME , & res ) == 0 )
80- info -> resolution = res .tv_sec + res .tv_nsec * 1e-9 ;
81- else
82- info -> resolution = 1e-9 ;
83- }
84- tp -> tv_sec = ts .tv_sec ;
85- tp -> tv_usec = ts .tv_nsec / 1000 ;
86- return ;
51+ err = clock_gettime (CLOCK_REALTIME , & ts );
52+ if (err ) {
53+ if (raise )
54+ PyErr_SetFromErrno (PyExc_OSError );
55+ return -1 ;
8756 }
88- #endif
57+ tp -> tv_sec = ts .tv_sec ;
58+ tp -> tv_usec = ts .tv_nsec / 1000 ;
59+
60+ if (info ) {
61+ struct timespec res ;
62+ info -> implementation = "clock_gettime(CLOCK_REALTIME)" ;
63+ info -> monotonic = 0 ;
64+ info -> adjustable = 1 ;
65+ if (clock_getres (CLOCK_REALTIME , & res ) == 0 )
66+ info -> resolution = res .tv_sec + res .tv_nsec * 1e-9 ;
67+ else
68+ info -> resolution = 1e-9 ;
69+ }
70+ return 0 ;
71+ #else /* HAVE_CLOCK_GETTIME */
8972
9073 /* test gettimeofday() */
9174#ifdef HAVE_GETTIMEOFDAY
@@ -94,51 +77,39 @@ pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info)
9477#else
9578 err = gettimeofday (tp , (struct timezone * )NULL );
9679#endif
97- if (err == 0 ) {
98- if (info ) {
99- info -> implementation = "gettimeofday()" ;
100- info -> resolution = 1e-6 ;
101- info -> monotonic = 0 ;
102- info -> adjustable = 1 ;
103- }
104- return ;
80+ if (err ) {
81+ if (raise )
82+ PyErr_SetFromErrno (PyExc_OSError );
83+ return -1 ;
10584 }
106- #endif /* HAVE_GETTIMEOFDAY */
10785
108- #ifdef HAVE_FTIME
109- ftime (& t );
110- tp -> tv_sec = t .time ;
111- tp -> tv_usec = t .millitm * 1000 ;
112- if (info ) {
113- info -> implementation = "ftime()" ;
114- info -> resolution = 1e-3 ;
115- info -> monotonic = 0 ;
116- info -> adjustable = 1 ;
117- }
118- #else /* !HAVE_FTIME */
119- tp -> tv_sec = time (NULL );
120- tp -> tv_usec = 0 ;
12186 if (info ) {
122- info -> implementation = "time ()" ;
123- info -> resolution = 1.0 ;
87+ info -> implementation = "gettimeofday ()" ;
88+ info -> resolution = 1e-6 ;
12489 info -> monotonic = 0 ;
12590 info -> adjustable = 1 ;
12691 }
127- #endif /* !HAVE_FTIME */
128-
129- #endif /* MS_WINDOWS */
92+ return 0 ;
93+ #endif /* HAVE_GETTIMEOFDAY */
94+ #endif /* !HAVE_CLOCK_GETTIME */
95+ #endif /* !MS_WINDOWS */
13096}
13197
13298void
13399_PyTime_gettimeofday (_PyTime_timeval * tp )
134100{
135- pygettimeofday (tp , NULL );
101+ if (pygettimeofday (tp , NULL , 0 ) < 0 ) {
102+ /* cannot happen, _PyTime_Init() checks that pygettimeofday() works */
103+ assert (0 );
104+ tp -> tv_sec = 0 ;
105+ tp -> tv_usec = 0 ;
106+ }
136107}
137108
138- void
109+ int
139110_PyTime_gettimeofday_info (_PyTime_timeval * tp , _Py_clock_info_t * info )
140111{
141- pygettimeofday (tp , info );
112+ return pygettimeofday (tp , info , 1 );
142113}
143114
144115static void
@@ -273,8 +244,12 @@ _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
273244 return _PyTime_ObjectToDenominator (obj , sec , usec , 1e6 , round );
274245}
275246
276- void
277- _PyTime_Init ()
247+ int
248+ _PyTime_Init (void )
278249{
279- /* Do nothing. Needed to force linking. */
250+ _PyTime_timeval tv ;
251+ /* ensure that the system clock works */
252+ if (_PyTime_gettimeofday_info (& tv , NULL ) < 0 )
253+ return -1 ;
254+ return 0 ;
280255}
0 commit comments