From 94e25967cda41b886e33ec254b917d21df21a187 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 15 Oct 1998 01:20:28 +0000 Subject: [PATCH] add --- lib/isc/unix/include/isc/time.h | 65 ++++++++++++++++++++++++ lib/isc/unix/time.c | 88 +++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 lib/isc/unix/include/isc/time.h create mode 100644 lib/isc/unix/time.c diff --git a/lib/isc/unix/include/isc/time.h b/lib/isc/unix/include/isc/time.h new file mode 100644 index 0000000000..bafab25b32 --- /dev/null +++ b/lib/isc/unix/include/isc/time.h @@ -0,0 +1,65 @@ + +#include + +/* + * This structure can be used both to represent absolute times, and to + * to represent intervals. + */ + +typedef struct os_time_t { + time_t seconds; + long nanoseconds; +} os_time_t; + +isc_result +os_time_get(os_time_t *timep); +/* + * Set *timep to the current absolute time (secs + nsec since January 1, 1970). + * + * Requires: + * + * 'timep' is a valid pointer. + * + * Returns: + * + * Success + * Unexpected error + */ + +int +os_time_compare(os_time_t *t1p, os_time_t *t2p); +/* + * Compare the times referenced by 't1p' and 't2p' + * + * Requires: + * + * 't1p' and 't2p' are a valid. + * + * Returns: + * + * -1 *tp1 < *t2p + * 0 *tp1 = *t2p + * 1 *tp1 > *t2p + */ + +void +os_time_add(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p); +/* + * Add 't1p' to 't2p', storing the result in 't3p'. + * + * Requires: + * + * 't1p', 't2p', and 't3p' are valid. + */ + +void +os_time_subtract(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p); +/* + * Subtract 't2p' from 't1p', storing the result in 't3p'. + * + * Requires: + * + * 't1p', 't2p', and 't3p' are valid. + * + * *tp1 >= *t2p + */ diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c new file mode 100644 index 0000000000..223ece1dc4 --- /dev/null +++ b/lib/isc/unix/time.c @@ -0,0 +1,88 @@ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +isc_result +os_time_get(os_time_t *timep) { + struct timeval tv; + + /* + * Set *timep to the current absolute time (secs + nsec since + * January 1, 1970). + */ + + REQUIRE(timep != NULL); + + if (gettimeofday(&tv, NULL) == -1) { + unexpected_error(__FILE__, __LINE__, strerror(errno)); + return (ISC_R_UNEXPECTED); + } + + timep->seconds = tv.tv_sec; + timep->nanoseconds = tv.tv_usec * 1000; + + return (ISC_R_SUCCESS); +} + +int +os_time_compare(os_time_t *t1p, os_time_t *t2p) { + /* + * Compare the times referenced by 't1p' and 't2p' + */ + + REQUIRE(t1p != NULL && t2p != NULL); + + if (t1p->seconds < t2p->seconds) + return (-1); + if (t1p->seconds > t2p->seconds) + return (1); + if (t1p->nanoseconds < t2p->nanoseconds) + return (-1); + if (t1p->nanoseconds > t2p->nanoseconds) + return (1); + return (0); +} + +void +os_time_add(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p) +{ + /* + * Add 't1p' to 't2p', storing the result in 't3p'. + */ + + REQUIRE(t1p != NULL && t2p != NULL && t3p != NULL); + + t3p->seconds = t1p->seconds + t2p->seconds; + t3p->nanoseconds = t1p->nanoseconds + t2p->nanoseconds; + if (t3p->nanoseconds > 1000000000) { + t3p->seconds++; + t3p->nanoseconds -= 1000000000; + } +} + +void +os_time_subtract(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p) { + /* + * Subtract 't2p' from 't1p', storing the result in 't1p'. + */ + + REQUIRE(t1p != NULL && t2p != NULL && t3p != NULL); + REQUIRE(os_time_compare(t1p, t2p) >= 0); + + t3p->seconds = t1p->seconds - t2p->seconds; + if (t1p->nanoseconds >= t2p->nanoseconds) + t3p->nanoseconds = t1p->nanoseconds - t2p->nanoseconds; + else { + t3p->nanoseconds = 1000000000 - t2p->nanoseconds + + t1p->nanoseconds; + t3p->seconds--; + } +}