1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Time handling functions
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
15 #if !(defined(HAVE_CLOCK_GETTIME) && HAVE_DECL_CLOCK_MONOTONIC)
16 # include <sys/time.h>
19 uint64_t opt_flush_timeout = 0;
22 static uint64_t start_time;
23 static uint64_t next_flush;
26 /// \brief Get the current time as milliseconds
28 /// It's relative to some point but not necessarily to the UNIX Epoch.
32 // NOTE: HAVE_DECL_CLOCK_MONOTONIC is always defined to 0 or 1.
33 #if defined(HAVE_CLOCK_GETTIME) && HAVE_DECL_CLOCK_MONOTONIC
34 // If CLOCK_MONOTONIC was available at compile time but for some
35 // reason isn't at runtime, fallback to CLOCK_REALTIME which
36 // according to POSIX is mandatory for all implementations.
37 static clockid_t clk_id = CLOCK_MONOTONIC;
39 while (clock_gettime(clk_id, &tv))
40 clk_id = CLOCK_REALTIME;
42 return (uint64_t)(tv.tv_sec) * UINT64_C(1000) + tv.tv_nsec / 1000000;
45 gettimeofday(&tv, NULL);
46 return (uint64_t)(tv.tv_sec) * UINT64_C(1000) + tv.tv_usec / 1000;
52 mytime_set_start_time(void)
54 start_time = mytime_now();
55 next_flush = start_time + opt_flush_timeout;
62 mytime_get_elapsed(void)
64 return mytime_now() - start_time;
69 mytime_set_flush_time(void)
71 next_flush = mytime_now() + opt_flush_timeout;
78 mytime_get_flush_timeout(void)
80 if (opt_flush_timeout == 0 || opt_mode != MODE_COMPRESS)
83 const uint64_t now = mytime_now();
84 if (now >= next_flush)
87 const uint64_t remaining = next_flush - now;
88 return remaining > INT_MAX ? INT_MAX : (int)remaining;