summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2011-09-22 20:33:22 +0200
committerStefan Metzmacher <metze@samba.org>2011-09-23 00:15:31 +0200
commit66f8070dd3a6a5c51c8e6d37deb7c52a9a717e1b (patch)
treea4201a369a93fe191a90a6a7d1e68b7585e8a072 /lib
parent1bb6e6758cffea967b6b8299553653cf4192f2e9 (diff)
downloadsamba-66f8070dd3a6a5c51c8e6d37deb7c52a9a717e1b.tar.gz
samba-66f8070dd3a6a5c51c8e6d37deb7c52a9a717e1b.tar.bz2
samba-66f8070dd3a6a5c51c8e6d37deb7c52a9a717e1b.zip
lib/util: move some timespec helpers from source3 to the toplevel
metze Autobuild-User: Stefan Metzmacher <metze@samba.org> Autobuild-Date: Fri Sep 23 00:15:31 CEST 2011 on sn-devel-104
Diffstat (limited to 'lib')
-rw-r--r--lib/util/time.c117
-rw-r--r--lib/util/time.h10
2 files changed, 127 insertions, 0 deletions
diff --git a/lib/util/time.c b/lib/util/time.c
index 31aa05cd0f..7216ea6e08 100644
--- a/lib/util/time.c
+++ b/lib/util/time.c
@@ -817,4 +817,121 @@ bool null_timespec(struct timespec ts)
ts.tv_sec == (time_t)-1;
}
+/****************************************************************************
+ Convert a normalized timeval to a timespec.
+****************************************************************************/
+struct timespec convert_timeval_to_timespec(const struct timeval tv)
+{
+ struct timespec ts;
+ ts.tv_sec = tv.tv_sec;
+ ts.tv_nsec = tv.tv_usec * 1000;
+ return ts;
+}
+
+/****************************************************************************
+ Convert a normalized timespec to a timeval.
+****************************************************************************/
+
+struct timeval convert_timespec_to_timeval(const struct timespec ts)
+{
+ struct timeval tv;
+ tv.tv_sec = ts.tv_sec;
+ tv.tv_usec = ts.tv_nsec / 1000;
+ return tv;
+}
+
+/****************************************************************************
+ Return a timespec for the current time
+****************************************************************************/
+
+struct timespec timespec_current(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_REALTIME, &ts);
+ return ts;
+}
+
+/****************************************************************************
+ Return the lesser of two timespecs.
+****************************************************************************/
+
+struct timespec timespec_min(const struct timespec *ts1,
+ const struct timespec *ts2)
+{
+ if (ts1->tv_sec < ts2->tv_sec) return *ts1;
+ if (ts1->tv_sec > ts2->tv_sec) return *ts2;
+ if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
+ return *ts2;
+}
+
+/****************************************************************************
+ compare two timespec structures.
+ Return -1 if ts1 < ts2
+ Return 0 if ts1 == ts2
+ Return 1 if ts1 > ts2
+****************************************************************************/
+
+int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
+{
+ if (ts1->tv_sec > ts2->tv_sec) return 1;
+ if (ts1->tv_sec < ts2->tv_sec) return -1;
+ if (ts1->tv_nsec > ts2->tv_nsec) return 1;
+ if (ts1->tv_nsec < ts2->tv_nsec) return -1;
+ return 0;
+}
+
+/****************************************************************************
+ Round up a timespec if nsec > 500000000, round down if lower,
+ then zero nsec.
+****************************************************************************/
+
+void round_timespec_to_sec(struct timespec *ts)
+{
+ ts->tv_sec = convert_timespec_to_time_t(*ts);
+ ts->tv_nsec = 0;
+}
+
+/****************************************************************************
+ Round a timespec to usec value.
+****************************************************************************/
+
+void round_timespec_to_usec(struct timespec *ts)
+{
+ struct timeval tv = convert_timespec_to_timeval(*ts);
+ *ts = convert_timeval_to_timespec(tv);
+ while (ts->tv_nsec > 1000000000) {
+ ts->tv_sec += 1;
+ ts->tv_nsec -= 1000000000;
+ }
+}
+
+/****************************************************************************
+ Put a 8 byte filetime from a struct timespec. Uses GMT.
+****************************************************************************/
+
+void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
+{
+ uint64_t d;
+
+ if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
+ *nt = 0;
+ return;
+ }
+ if (ts.tv_sec == TIME_T_MAX) {
+ *nt = 0x7fffffffffffffffLL;
+ return;
+ }
+ if (ts.tv_sec == (time_t)-1) {
+ *nt = (uint64_t)-1;
+ return;
+ }
+
+ d = ts.tv_sec;
+ d += TIME_FIXUP_CONSTANT_INT;
+ d *= 1000*1000*10;
+ /* d is now in 100ns units. */
+ d += (ts.tv_nsec / 100);
+
+ *nt = d;
+}
diff --git a/lib/util/time.h b/lib/util/time.h
index 204c261c1d..047daecdbf 100644
--- a/lib/util/time.h
+++ b/lib/util/time.h
@@ -300,4 +300,14 @@ struct timespec convert_time_t_to_timespec(time_t t);
bool null_timespec(struct timespec ts);
+struct timespec convert_timeval_to_timespec(const struct timeval tv);
+struct timeval convert_timespec_to_timeval(const struct timespec ts);
+struct timespec timespec_current(void);
+struct timespec timespec_min(const struct timespec *ts1,
+ const struct timespec *ts2);
+int timespec_compare(const struct timespec *ts1, const struct timespec *ts2);
+void round_timespec_to_sec(struct timespec *ts);
+void round_timespec_to_usec(struct timespec *ts);
+void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts);
+
#endif /* _SAMBA_TIME_H_ */