]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: add timespec_store_nsec()
authorLennart Poettering <lennart@poettering.net>
Thu, 27 Aug 2020 17:01:48 +0000 (19:01 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 28 Aug 2020 12:22:43 +0000 (14:22 +0200)
timespec_store_nsec() is to timespec_store() what timespec_load_nsec()
is to timespec_load(), i.e. the nsec version of the usual usec API

src/basic/time-util.c
src/basic/time-util.h

index 0958f251eaf3f46b7750e5b0ce60cc8f1524700b..7fa3b486237606bf22be69daf7a36d2f879c138a 100644 (file)
@@ -244,12 +244,28 @@ struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
         if (u == USEC_INFINITY ||
             u / USEC_PER_SEC >= TIME_T_MAX) {
                 ts->tv_sec = (time_t) -1;
-                ts->tv_nsec = (long) -1;
+                ts->tv_nsec = -1L;
                 return ts;
         }
 
         ts->tv_sec = (time_t) (u / USEC_PER_SEC);
-        ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
+        ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
+
+        return ts;
+}
+
+struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n)  {
+        assert(ts);
+
+        if (n == NSEC_INFINITY ||
+            n / NSEC_PER_SEC >= TIME_T_MAX) {
+                ts->tv_sec = (time_t) -1;
+                ts->tv_nsec = -1L;
+                return ts;
+        }
+
+        ts->tv_sec = (time_t) (n / NSEC_PER_SEC);
+        ts->tv_nsec = (long) (n % NSEC_PER_SEC);
 
         return ts;
 }
index b181a6b3b4586ce2f7dd0f5191119ac8fc0d5777..cecd5efa604ae07970265e91a02332880646c5aa 100644 (file)
@@ -112,6 +112,7 @@ usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock);
 usec_t timespec_load(const struct timespec *ts) _pure_;
 nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
 struct timespec *timespec_store(struct timespec *ts, usec_t u);
+struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n);
 
 usec_t timeval_load(const struct timeval *tv) _pure_;
 struct timeval *timeval_store(struct timeval *tv, usec_t u);