[lib] timestamp_now(): gettimeofday() if needed

Use gettimeofday() when clock_gettime() is not available.
This commit is contained in:
Matteo Cypriani 2012-02-02 20:27:48 +01:00
parent e98474e678
commit 9f8c85d9c7
1 changed files with 14 additions and 1 deletions

View File

@ -15,6 +15,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <sys/time.h>
#include <signal.h> #include <signal.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -177,14 +178,26 @@ int owl_msleep(uint32_t time_ms)
int owl_timestamp_now(owl_timestamp *const now) int owl_timestamp_now(owl_timestamp *const now)
{ {
int ret ; int ret ;
#if _POSIX_TIMERS > 0
struct timespec now_ts ; struct timespec now_ts ;
if ((ret = clock_gettime(CLOCK_REALTIME, &now_ts))) ret = clock_gettime(CLOCK_REALTIME, &now_ts) ;
#else // _POSIX_TIMERS
struct timeval now_ts ;
ret = gettimeofday(&now_ts, NULL) ;
#endif // _POSIX_TIMERS
if (ret)
{ {
perror("Cannot get the current time") ; perror("Cannot get the current time") ;
return ret ; return ret ;
} }
#if _POSIX_TIMERS > 0
owl_timespec_to_timestamp(&now_ts, now) ; owl_timespec_to_timestamp(&now_ts, now) ;
#else // _POSIX_TIMERS
owl_timeval_to_timestamp(&now_ts, now) ;
#endif // _POSIX_TIMERS
return 0 ; return 0 ;
} }