From 0af9072793e53b9c174cc7022d6313dea7ec307a Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Fri, 5 Dec 2014 22:28:39 -0500 Subject: systemtime: Use gettimeofday if POSIX timers not available --- src/systemtime.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src/systemtime.c') diff --git a/src/systemtime.c b/src/systemtime.c index abbb5ae..12b74cc 100644 --- a/src/systemtime.c +++ b/src/systemtime.c @@ -14,13 +14,17 @@ You should have received a copy of the GNU General Public License along with Redshift. If not, see . - Copyright (c) 2010 Jon Lund Steffensen + Copyright (c) 2010-2014 Jon Lund Steffensen */ #include #ifndef _WIN32 -# include +# ifdef _POSIX_TIMERS +# include +# else +# include +# endif #endif #include "systemtime.h" @@ -28,7 +32,16 @@ int systemtime_get_time(double *t) { -#ifndef _WIN32 +#if defined(_WIN32) /* Windows */ + FILETIME now; + ULARGE_INTEGER i; + GetSystemTimeAsFileTime(&now); + i.LowPart = now.dwLowDateTime; + i.HighPart = now.dwHighDateTime; + + /* FILETIME is tenths of microseconds since 1601-01-01 UTC */ + *t = (i.QuadPart / 10000000.0) - 11644473600.0; +#elif defined(_POSIX_TIMERS) /* POSIX timers */ struct timespec now; int r = clock_gettime(CLOCK_REALTIME, &now); if (r < 0) { @@ -37,16 +50,16 @@ systemtime_get_time(double *t) } *t = now.tv_sec + (now.tv_nsec / 1000000000.0); -#else /* _WIN32 */ - FILETIME now; - ULARGE_INTEGER i; - GetSystemTimeAsFileTime(&now); - i.LowPart = now.dwLowDateTime; - i.HighPart = now.dwHighDateTime; +#else /* other platforms */ + struct timeval now; + int r = gettimeofday(&now, NULL); + if (r < 0) { + perror("gettimeofday"); + return -1; + } - /* FILETIME is tenths of microseconds since 1601-01-01 UTC */ - *t = (i.QuadPart / 10000000.0) - 11644473600.0; -#endif /* _WIN32 */ + *t = now.tv_sec + (now.tv_usec / 1000000.0); +#endif return 0; } -- cgit v1.2.3-70-g09d2