]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: add full_timespec_to_time_t()
authorRalph Boehme <slow@samba.org>
Wed, 4 Dec 2019 11:46:11 +0000 (12:46 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 6 Dec 2019 00:17:35 +0000 (00:17 +0000)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=7771

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/time.c
lib/util/time.h

index 8e68662dfa309b74248bec3534cd0a8fb42c29c7..05918a94ea5f3b654ca551eb096bdda7d6125ea5 100644 (file)
@@ -1080,3 +1080,39 @@ struct timespec nt_time_to_full_timespec(NTTIME nt)
        ret.tv_sec = (time_t)d;
        return ret;
 }
+
+/**
+ * Note: this function uses the full time_t range as valid date values including
+ * (time_t)0 and -1. That means that struct timespec sentinel values (cf
+ * is_omit_timespec()) can't be converted to sentinel values in a time_t
+ * representation. Callers should therefor check the NTTIME value with
+ * null_nttime() before calling this function.
+ **/
+time_t full_timespec_to_time_t(const struct timespec *_ts)
+{
+       struct timespec ts = *_ts;
+
+       if (is_omit_timespec(_ts)) {
+               /*
+                * Unfortunately there's no sensible sentinel value in the
+                * time_t range that is not conflicting with a valid time value
+                * ((time_t)0 and -1 are valid time values). Bite the bullit and
+                * return 0.
+                */
+               return 0;
+       }
+
+       /* Ensure tv_nsec is less than 1sec. */
+       while (ts.tv_nsec > 1000000000) {
+               ts.tv_sec += 1;
+               ts.tv_nsec -= 1000000000;
+       }
+
+       /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
+          increment if it's greater than 500 millionth of a second. */
+
+       if (ts.tv_nsec > 500000000) {
+               return ts.tv_sec + 1;
+       }
+       return ts.tv_sec;
+}
index 29c8001485f983e1a68afcb60262ba100ad2dffe..b9bd67499f6ba3b103f7ef0fecbf564da4385a39 100644 (file)
@@ -343,5 +343,6 @@ bool is_omit_timespec(const struct timespec *ts);
 struct timespec make_omit_timespec(void);
 NTTIME full_timespec_to_nt_time(const struct timespec *ts);
 struct timespec nt_time_to_full_timespec(NTTIME nt);
+time_t full_timespec_to_time_t(const struct timespec *ts);
 
 #endif /* _SAMBA_TIME_H_ */