| 1 | #include "cgreen/internal/cgreen_time.h" |
| 2 | #include <stdint.h> |
| 3 | #include <stdio.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #ifdef __ANDROID__ |
| 7 | #include "cgreen/internal/android_headers/androidcompat.h" |
| 8 | #endif // #ifdef __ANDROID__ |
| 9 | |
| 10 | /* TODO: this should really be handle by CMake config... */ |
| 11 | #if defined(__FreeBSD__) || defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__OpenBSD__) |
| 12 | # include <sys/time.h> |
| 13 | # define HAVE_GETTIMEOFDAY 1 |
| 14 | #else |
| 15 | # error "Your POSIX platform does not support gettimeofday(). Please report a bug to http://github.com/cgreen-devs/cgreen/issues" |
| 16 | #endif |
| 17 | |
| 18 | #if defined(HAVE_GETTIMEOFDAY) |
| 19 | uint32_t cgreen_time_get_current_milliseconds(void) { |
| 20 | #ifdef __CYGWIN__ |
| 21 | /* TODO: This is actually the POSIX recommended way to do this */ |
| 22 | struct timespec ts; |
| 23 | if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { |
| 24 | fprintf(stderr, "cgreen error: could not get time\n"); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | return ts.tv_sec * 1000u + ts.tv_nsec / 1000000u; |
| 29 | #else |
| 30 | struct timeval tv; |
| 31 | if (gettimeofday(&tv, NULL) != 0) { |
| 32 | fprintf(stderr, "cgreen error: could not get time of day\n"); |
| 33 | return 0; |
| 34 | } |
| 35 | return tv.tv_sec * 1000u + tv.tv_usec / 1000u; |
| 36 | #endif |
| 37 | } |
| 38 | #endif |
| 39 | |
| 40 | /* vim: set ts=4 sw=4 et cindent: */ |