2626
2727#define MAX_PATH_LEN 1024
2828
29- static void get_time_info (int32_t * tv_sec , int32_t * tv_usec )
29+ static void get_time_info (int32_t * tv_sec , int32_t * tv_nsec )
3030{
3131#if defined(HAVE_POSIX_TIMER )
3232 struct timespec t ;
3333 clock_gettime (CLOCKID , & t );
3434 * tv_sec = t .tv_sec ;
35- * tv_usec = t .tv_nsec / 1000 ;
35+ * tv_nsec = t .tv_nsec ;
3636#elif defined(HAVE_MACH_TIMER )
3737 static mach_timebase_info_data_t info ;
3838 /* If it is the first time running, obtain the timebase. Using denom == 0
@@ -43,33 +43,28 @@ static void get_time_info(int32_t *tv_sec, int32_t *tv_usec)
4343 /* Hope that the multiplication doesn't overflow. */
4444 uint64_t nsecs = mach_absolute_time () * info .numer / info .denom ;
4545 * tv_sec = nsecs / 1e9 ;
46- * tv_usec = ( nsecs / 1e3 ) - (* tv_sec * 1e6 );
46+ * tv_nsec = nsecs - (* tv_sec * 1e9 );
4747#else /* low resolution timer */
4848 clock_t t = clock ();
4949 * tv_sec = t / CLOCKS_PER_SEC ;
50- * tv_usec = (t % CLOCKS_PER_SEC ) * (1e6 / CLOCKS_PER_SEC );
50+ * tv_nsec = (t % CLOCKS_PER_SEC ) * (1e9 / CLOCKS_PER_SEC );
5151#endif
5252}
5353
5454void rv_gettimeofday (struct timeval * tv )
5555{
56- int32_t tv_sec , tv_usec ;
57- get_time_info (& tv_sec , & tv_usec );
56+ int32_t tv_sec , tv_nsec ;
57+ get_time_info (& tv_sec , & tv_nsec );
5858 tv -> tv_sec = tv_sec ;
59- tv -> tv_usec = tv_usec ;
59+ tv -> tv_usec = tv_nsec / 1000 ;
6060}
6161
62- /* TODO: Clarify newlib's handling of time units.
63- * It appears that newlib is using millisecond resolution for time manipulation,
64- * while clock_gettime expects nanoseconds in the timespec struct.
65- * Further investigation are needed.
66- */
6762void rv_clock_gettime (struct timespec * tp )
6863{
69- int32_t tv_sec , tv_usec ;
70- get_time_info (& tv_sec , & tv_usec );
64+ int32_t tv_sec , tv_nsec ;
65+ get_time_info (& tv_sec , & tv_nsec );
7166 tp -> tv_sec = tv_sec ;
72- tp -> tv_nsec = tv_usec * 1000 ; /* Transfer to microseconds */
67+ tp -> tv_nsec = tv_nsec ; /* Transfer to microseconds */
7368}
7469
7570char * sanitize_path (const char * input )
0 commit comments