]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: add nt_time_to_full_timespec()
authorRalph Boehme <slow@samba.org>
Thu, 5 Dec 2019 14:00:19 +0000 (15:00 +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 67308444daeb27489db47143b5b194c538cb0ccd..8e68662dfa309b74248bec3534cd0a8fb42c29c7 100644 (file)
@@ -1039,3 +1039,44 @@ NTTIME full_timespec_to_nt_time(const struct timespec *ts)
 
        return d;
 }
+
+/**
+ * Like nt_time_to_unix_timespec() but allowing negative tv_sec values and
+ * returning NTTIME=0 and -1 as struct timespec {.tv_nsec = SAMBA_UTIME_OMIT}.
+ *
+ * See also: is_omit_timespec().
+ **/
+struct timespec nt_time_to_full_timespec(NTTIME nt)
+{
+       int64_t d;
+       struct timespec ret;
+
+       if ((nt == 0) || (nt == (int64_t)-1)) {
+               return (struct timespec){.tv_nsec = SAMBA_UTIME_OMIT};
+       }
+
+       d = (int64_t)nt;
+       /* d is now in 100ns units, since jan 1st 1601".
+          Save off the ns fraction. */
+
+       /*
+        * Take the last seven decimal digits and multiply by 100.
+        * to convert from 100ns units to 1ns units.
+        */
+        ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
+
+       /* Convert to seconds */
+       d /= 1000*1000*10;
+
+       /* Now adjust by 369 years to make the secs since 1970 */
+       d -= TIME_FIXUP_CONSTANT_INT;
+
+       if (d >= (int64_t)TIME_T_MAX) {
+               ret.tv_sec = TIME_T_MAX;
+               ret.tv_nsec = 0;
+               return ret;
+       }
+
+       ret.tv_sec = (time_t)d;
+       return ret;
+}
index 5c7b7b209ece4742906b168b550fb6d66db051e3..29c8001485f983e1a68afcb60262ba100ad2dffe 100644 (file)
@@ -342,5 +342,6 @@ NTTIME unix_timespec_to_nt_time(struct timespec ts);
 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);
 
 #endif /* _SAMBA_TIME_H_ */