summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas De Marchi <lucas.de.marchi@gmail.com>2022-06-03 14:50:44 -0700
committerLucas De Marchi <lucas.de.marchi@gmail.com>2022-06-26 23:23:46 -0700
commitba105fafed8ccabc0a427049a14e093b914f7ddc (patch)
tree2978fc24c184d449cfd57cc62c99736409712d28
parent5622f1dae10cf393bc1b142573ce5bf446334816 (diff)
downloadkmod-ba105fafed8ccabc0a427049a14e093b914f7ddc.tar.gz
kmod-ba105fafed8ccabc0a427049a14e093b914f7ddc.tar.bz2
kmod-ba105fafed8ccabc0a427049a14e093b914f7ddc.zip
util: Add msec variants for time-related functions
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
-rw-r--r--shared/util.c16
-rw-r--r--shared/util.h4
2 files changed, 20 insertions, 0 deletions
diff --git a/shared/util.c b/shared/util.c
index aeb171f..d4452eb 100644
--- a/shared/util.c
+++ b/shared/util.c
@@ -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
diff --git a/shared/util.h b/shared/util.h
index 734a523..bedafa3 100644
--- a/shared/util.h
+++ b/shared/util.h
@@ -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 */
/* ************************************************************************ */