From dc68ce59a84cafbf1eb2e5b2a98150f366c28ec9 Mon Sep 17 00:00:00 2001 From: "Seungyoung \"Steve\" Kim" Date: Mon, 30 Mar 2015 09:17:30 -0700 Subject: [PATCH] This fixes #33. Compatibility fix - Change clock_gettime to gettimeofday. --- src/utilities/qstring.c | 19 +++++++-------- src/utilities/qtime.c | 52 ++++++----------------------------------- tests/qunit.h | 13 ++++------- 3 files changed, 20 insertions(+), 64 deletions(-) diff --git a/src/utilities/qstring.c b/src/utilities/qstring.c index 6fcb2e08..6bd4e640 100644 --- a/src/utilities/qstring.c +++ b/src/utilities/qstring.c @@ -36,8 +36,8 @@ #include #include #include -#include #include +#include #include "qinternal.h" #include "utilities/qencode.h" #include "utilities/qhash.h" @@ -651,23 +651,20 @@ qlist_t *qstrtokenizer(const char *str, const char *delimiters) { * it uses rand(). */ char *qstrunique(const char *seed) { - int time1; - long time2; + long usec; #ifdef _WIN32 FILETIME ft; GetSystemTimeAsFileTime(&ft); - time1 = (int)time(NULL); - time2 = (long)ft.dwLowDateTime; + usec = ft.dwLowDateTime % 1000000; #else - struct timespec tv; - clock_gettime(CLOCK_MONOTONIC, &tv); - time1 = (int) tv.tv_sec; - time2 = (long) tv.tv_nsec; + struct timeval tv; + gettimeofday(&tv, NULL); + usec = tv.tv_usec; #endif char uniquestr[128]; - snprintf(uniquestr, sizeof(uniquestr), "%u%d%d%ld%s", getpid(), rand(), - time1, time2, (seed != NULL) ? seed : ""); + snprintf(uniquestr, sizeof(uniquestr), "%u%d%lu%ld%s", getpid(), rand(), + (unsigned long)time(NULL), usec, (seed != NULL) ? seed : ""); unsigned char md5hash[16]; qhashmd5(uniquestr, strlen(uniquestr), md5hash); diff --git a/src/utilities/qtime.c b/src/utilities/qtime.c index 55e5e911..826c6d01 100644 --- a/src/utilities/qtime.c +++ b/src/utilities/qtime.c @@ -30,64 +30,26 @@ * @file qtime.c Time handling APIs. */ -#define __USE_XOPEN -#define _XOPEN_SOURCE -#define _BSD_SOURCE +//#define __USE_XOPEN +//#define _XOPEN_SOURCE +//#define _BSD_SOURCE #include #include #include #include -#include +#include #include "qinternal.h" #include "utilities/qtime.h" -/** - * Calculate time difference between 2 timespec structures. - * - * @param t1 start time - * @param t2 end time - * @param diff a pointer of timespec structure for storing the time difference. - */ -void qtime_timespec_diff(struct timespec t1, struct timespec t2, - struct timespec *diff) { - diff->tv_sec = t2.tv_sec - t1.tv_sec; - if (t2.tv_nsec >= t1.tv_nsec) { - diff->tv_sec = t2.tv_sec - t1.tv_sec; - diff->tv_nsec = t2.tv_nsec - t1.tv_nsec; - } else { - diff->tv_sec = t2.tv_sec - t1.tv_sec - 1; - diff->tv_nsec = 1000000000 + t2.tv_nsec - t1.tv_nsec; - } -} - -/** - * Calculate time difference between 2 timeval structures. - * - * @param t1 start time - * @param t2 end time - * @param diff a pointer of timeval structure for storing the time difference. - */ -void qtime_timeval_diff(struct timeval t1, struct timeval t2, - struct timeval *diff) { - diff->tv_sec = t2.tv_sec - t1.tv_sec; - if (t2.tv_usec >= t1.tv_usec) { - diff->tv_sec = t2.tv_sec - t1.tv_sec; - diff->tv_usec = t2.tv_usec - t1.tv_usec; - } else { - diff->tv_sec = t2.tv_sec - t1.tv_sec - 1; - diff->tv_usec = 1000000 + t2.tv_usec - t1.tv_usec; - } -} - /** * Returns the current time in milliseconds. * * @return current time in milliseconds. */ long qtime_current_milli(void) { - struct timespec tv; - clock_gettime(CLOCK_REALTIME, &tv); - long time = (tv.tv_sec * 1000) + (tv.tv_nsec / 1000000); + struct timeval tv; + gettimeofday(&tv, NULL); + long time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); return time; } diff --git a/tests/qunit.h b/tests/qunit.h index f3ccbfaf..47655726 100644 --- a/tests/qunit.h +++ b/tests/qunit.h @@ -61,7 +61,7 @@ int _q_this_failed = 0; \ int _q_errcnt = 0; \ int _q_assert_cnt = 0; /* number of assert test in a test */ \ int _q_assert_dot_cnt = 0; /* number of dots printed out in a test. */ \ -struct timespec _q_timer; \ +long _q_timer; \ int main(int argc, char **argv) { \ PRINTLN("%s", _q_title); \ PRINTLN("======================================================================"); \ @@ -85,9 +85,8 @@ int main(int argc, char **argv) { \ #define _TEST_RESULT() \ TIMER_STOP(_q_timer); \ - if (_q_tot_tests ) PRINTLN(" %s (%d assertions, %ldus)", \ - (_q_this_failed) ? "FAIL" : "OK", _q_assert_cnt, \ - ((_q_timer.tv_sec * 1000000000) + _q_timer.tv_nsec) / 1000); \ + if (_q_tot_tests ) PRINTLN(" %s (%d assertions, %ldms)", \ + (_q_this_failed) ? "FAIL" : "OK", _q_assert_cnt, _q_timer); \ _q_tot_failed += (_q_this_failed) ? 1 : 0; \ _q_this_failed = 0; @@ -112,13 +111,11 @@ int main(int argc, char **argv) { \ #define ASSERT_FALSE(b) ASSERT(!(b)) #define TIMER_START(x) do { \ - clock_gettime(CLOCK_MONOTONIC, &x); \ + x = qtime_current_milli(); \ } while(0) #define TIMER_STOP(x) do { \ - struct timespec _tp1 = x, _tp2; \ - clock_gettime(CLOCK_MONOTONIC, &_tp2); \ - qtime_timespec_diff(_tp1, _tp2, &x); \ + x = qtime_current_milli() - x; \ } while(0) #ifdef __cplusplus