X-Git-Url: https://unix4lyfe.org/gitweb/darkstat-debian/blobdiff_plain/3c6b3682c70be84db86e70c191016913a9836c31..HEAD:/now.c diff --git a/now.c b/now.c index 95ab2a2..b1a43cb 100644 --- a/now.c +++ b/now.c @@ -23,41 +23,41 @@ #include #include -#ifdef __APPLE__ +#if defined(__MACH__) && !defined(__gnu_hurd__) /* Fake up clock_gettime() on OS X. */ -# include -# include -# include -# include - -typedef int clockid_t; -#define CLOCK_REALTIME 0 -#define CLOCK_MONOTONIC 1 - -static uint64_t mono_first = 0; - -int clock_gettime(clockid_t clk_id, struct timespec *tp) { - if (clk_id == CLOCK_REALTIME) { - struct timeval tv; - gettimeofday(&tv, NULL); - tp->tv_sec = tv.tv_sec; - tp->tv_nsec = tv.tv_usec * 1000; - return 0; - } - if (clk_id == CLOCK_MONOTONIC) { - uint64_t t = mach_absolute_time(); - mach_timebase_info_data_t timebase; - mach_timebase_info(&timebase); - if (!mono_first) { - mono_first = t; +# include +# include +# include +# include + + typedef int clockid_t; +# define CLOCK_REALTIME 0 +# define CLOCK_MONOTONIC 1 + + static uint64_t mono_first = 0; + + int clock_gettime(clockid_t clk_id, struct timespec *tp) { + if (clk_id == CLOCK_REALTIME) { + struct timeval tv; + gettimeofday(&tv, NULL); + tp->tv_sec = tv.tv_sec; + tp->tv_nsec = tv.tv_usec * 1000; + return 0; + } + if (clk_id == CLOCK_MONOTONIC) { + uint64_t t = mach_absolute_time(); + mach_timebase_info_data_t timebase; + mach_timebase_info(&timebase); + if (!mono_first) { + mono_first = t; + } + uint64_t tdiff = (t - mono_first) * timebase.numer / timebase.denom; + tp->tv_sec = tdiff / 1000000000; + tp->tv_nsec = tdiff % 1000000000; + return 0; } - uint64_t tdiff = (t - mono_first) * timebase.numer / timebase.denom; - tp->tv_sec = tdiff / 1000000000; - tp->tv_nsec = tdiff % 1000000000; - return 0; + return -1; } - return -1; -} #endif /* __MACH__ */ static struct timespec clock_real, clock_mono; @@ -120,14 +120,14 @@ void now_update(void) { all_clocks_update(); } -time_t mono_to_real(const time_t t) { +time_t mono_to_real(const int64_t t) { assert(now_initialized); - return t - clock_mono.tv_sec + clock_real.tv_sec; + return (time_t)(t - (int64_t)clock_mono.tv_sec + (int64_t)clock_real.tv_sec); } -time_t real_to_mono(const time_t t) { +int64_t real_to_mono(const time_t t) { assert(now_initialized); - return t - clock_real.tv_sec + clock_mono.tv_sec; + return (int64_t)(t - clock_real.tv_sec + clock_mono.tv_sec); } void timer_start(struct timespec *t) {