]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
util: Add msec variants for time-related functions
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 3 Jun 2022 21:50:44 +0000 (14:50 -0700)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 27 Jun 2022 06:23:46 +0000 (23:23 -0700)
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
shared/util.c
shared/util.h

index aeb171f18ed42879cf8dc8b66f857fa58f9db0d9..d4452eb3589d4408a2974195c6c5e594d53fd8af 100644 (file)
@@ -466,6 +466,12 @@ unsigned long long ts_usec(const struct timespec *ts)
               (unsigned long long) ts->tv_nsec / NSEC_PER_USEC;
 }
 
+unsigned long long ts_msec(const struct timespec *ts)
+{
+       return (unsigned long long) ts->tv_sec * MSEC_PER_SEC +
+              (unsigned long long) ts->tv_nsec / NSEC_PER_MSEC;
+}
+
 unsigned long long now_usec(void)
 {
        struct timespec ts;
@@ -476,6 +482,16 @@ unsigned long long now_usec(void)
        return ts_usec(&ts);
 }
 
+unsigned long long now_msec(void)
+{
+       struct timespec ts;
+
+       if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
+               return 0;
+
+       return ts_msec(&ts);
+}
+
 unsigned long long stat_mstamp(const struct stat *st)
 {
 #ifdef HAVE_STRUCT_STAT_ST_MTIM
index 734a5234774fb7cf0d3c5aa68dcafc0a1435c5d6..bedafa38094edb77c3ec097b34017fec340ad3cb 100644 (file)
@@ -48,9 +48,13 @@ unsigned long long stat_mstamp(const struct stat *st);
  * ************************************************************************ */
 #define USEC_PER_SEC   1000000ULL
 #define USEC_PER_MSEC  1000ULL
+#define MSEC_PER_SEC   1000ULL
+#define NSEC_PER_MSEC  1000000ULL
 
 unsigned long long ts_usec(const struct timespec *ts);
+unsigned long long ts_msec(const struct timespec *ts);
 unsigned long long now_usec(void);
+unsigned long long now_msec(void);
 
 /* endianess and alignments                                                 */
 /* ************************************************************************ */