From 66f8070dd3a6a5c51c8e6d37deb7c52a9a717e1b Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 22 Sep 2011 20:33:22 +0200 Subject: lib/util: move some timespec helpers from source3 to the toplevel metze Autobuild-User: Stefan Metzmacher Autobuild-Date: Fri Sep 23 00:15:31 CEST 2011 on sn-devel-104 --- source3/lib/time.c | 146 ----------------------------------------------------- 1 file changed, 146 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/time.c b/source3/lib/time.c index db9ec0a34f..7fe53928ab 100644 --- a/source3/lib/time.c +++ b/source3/lib/time.c @@ -263,95 +263,6 @@ time_t srv_make_unix_date3(const void *date_ptr) return make_unix_date3(date_ptr, server_zone_offset); } -/**************************************************************************** - 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; - } -} - /**************************************************************************** Interprets an nt time into a unix struct timespec. Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff @@ -468,63 +379,6 @@ time_t uint64s_nt_time_to_unix_abs(const uint64_t *src) return nt_time_to_unix_abs(&nttime); } -/**************************************************************************** - 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; -} - -#if 0 -void nt_time_to_unix_timespec(struct timespec *ts, NTTIME t) -{ - if (ts == NULL) { - return; - } - - /* t starts in 100 nsec units since 1601-01-01. */ - - t *= 100; - /* t is now in nsec units since 1601-01-01. */ - - t -= TIME_FIXUP_CONSTANT*1000*1000*100; - /* t is now in nsec units since the UNIX epoch 1970-01-01. */ - - ts->tv_sec = t / 1000000000LL; - - if (TIME_T_MIN > ts->tv_sec || ts->tv_sec > TIME_T_MAX) { - ts->tv_sec = 0; - ts->tv_nsec = 0; - return; - } - - ts->tv_nsec = t - ts->tv_sec*1000000000LL; -} -#endif - /**************************************************************************** Convert a time_t to a NTTIME structure -- cgit