Skip to content

Commit

Permalink
This fixes #33. Compatibility fix - Change clock_gettime to gettimeof…
Browse files Browse the repository at this point in the history
…day.
  • Loading branch information
wolkykim committed Mar 30, 2015
1 parent cdfec11 commit dc68ce5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 64 deletions.
19 changes: 8 additions & 11 deletions src/utilities/qstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include "qinternal.h"
#include "utilities/qencode.h"
#include "utilities/qhash.h"
Expand Down Expand Up @@ -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);
Expand Down
52 changes: 7 additions & 45 deletions src/utilities/qtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#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;
}

Expand Down
13 changes: 5 additions & 8 deletions tests/qunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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("======================================================================"); \
Expand All @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit dc68ce5

Please sign in to comment.