]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
contrib/time: add millisecond time functions
authorLibor Peltan <libor.peltan@nic.cz>
Tue, 9 Sep 2025 07:08:11 +0000 (09:08 +0200)
committerDavid Vašek <david.vasek@nic.cz>
Thu, 11 Sep 2025 15:42:23 +0000 (17:42 +0200)
src/contrib/time.c
src/contrib/time.h

index 1ef6a2789c24863f6d9ac44bce023bdb62b22910..50ab799a270f10ab3d65999ed379980c7e648641 100644 (file)
@@ -45,6 +45,28 @@ double time_diff_ms(const struct timespec *begin, const struct timespec *end)
        return (result.tv_sec * 1e3) + (result.tv_nsec / 1e6);
 }
 
+knot_millis_t knot_millis_from_timespec(struct timespec *ts)
+{
+       return ts->tv_sec * 1000LLU + ts->tv_nsec / 1000000LLU;
+}
+
+struct timespec knot_millis_to_timespec(knot_millis_t ms)
+{
+       return (struct timespec){ .tv_sec = ms / 1000LLU, .tv_nsec = (ms % 1000LLU) * 1000000LLU };
+}
+
+knot_millis_t knot_millis_now(void)
+{
+       struct timespec ts = time_now();
+       return knot_millis_from_timespec(&ts);
+}
+
+void knot_millis_sleep(knot_millis_t ms)
+{
+       struct timespec ts = knot_millis_to_timespec(ms);
+       nanosleep(&ts, NULL);
+}
+
 typedef struct {
        const char *format;
        const char *timespec;
index 6a353f673d6f3f70916a86d6b787829c1bf7650d..07cdd2465d2705e11c4947914f7ec440ff81f461 100644 (file)
@@ -15,6 +15,8 @@
  #define st_mtim st_mtimespec
 #endif
 
+typedef unsigned long long knot_millis_t;
+
 /*!
  * \brief Specify output format for knot_time_print().
  */
@@ -41,6 +43,14 @@ struct timespec time_diff(const struct timespec *begin, const struct timespec *e
  */
 double time_diff_ms(const struct timespec *begin, const struct timespec *end);
 
+knot_millis_t knot_millis_from_timespec(struct timespec *ts);
+
+struct timespec knot_millis_to_timespec(knot_millis_t ms);
+
+knot_millis_t knot_millis_now(void);
+
+void knot_millis_sleep(knot_millis_t ms);
+
 /*!
  * \brief Data type for keeping UNIX timestamps.
  *