2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 02:28:55 +00:00
bind/lib/isc/unix/time.c

90 lines
1.8 KiB
C
Raw Normal View History

1998-10-15 01:20:28 +00:00
1998-10-22 01:59:50 +00:00
#include <sys/time.h>
1998-10-15 01:20:28 +00:00
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <isc/assertions.h>
#include <isc/unexpect.h>
#include <isc/time.h>
isc_result
1998-10-22 01:33:20 +00:00
isc_time_get(isc_time_t timep) {
1998-10-15 01:20:28 +00:00
struct timeval tv;
/*
* Set *timep to the current absolute time (secs + nsec since
* January 1, 1970).
*/
REQUIRE(timep != NULL);
if (gettimeofday(&tv, NULL) == -1) {
1998-10-21 01:13:50 +00:00
UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
1998-10-15 01:20:28 +00:00
return (ISC_R_UNEXPECTED);
}
timep->seconds = tv.tv_sec;
timep->nanoseconds = tv.tv_usec * 1000;
return (ISC_R_SUCCESS);
}
int
1998-10-22 01:33:20 +00:00
isc_time_compare(isc_time_t t1, isc_time_t t2) {
1998-10-15 01:20:28 +00:00
/*
1998-10-22 01:33:20 +00:00
* Compare the times referenced by 't1' and 't2'
1998-10-15 01:20:28 +00:00
*/
1998-10-22 01:33:20 +00:00
REQUIRE(t1 != NULL && t2 != NULL);
1998-10-15 01:20:28 +00:00
1998-10-22 01:33:20 +00:00
if (t1->seconds < t2->seconds)
1998-10-15 01:20:28 +00:00
return (-1);
1998-10-22 01:33:20 +00:00
if (t1->seconds > t2->seconds)
1998-10-15 01:20:28 +00:00
return (1);
1998-10-22 01:33:20 +00:00
if (t1->nanoseconds < t2->nanoseconds)
1998-10-15 01:20:28 +00:00
return (-1);
1998-10-22 01:33:20 +00:00
if (t1->nanoseconds > t2->nanoseconds)
1998-10-15 01:20:28 +00:00
return (1);
return (0);
}
void
1998-10-22 01:33:20 +00:00
isc_time_add(isc_time_t t1, isc_time_t t2, isc_time_t t3)
1998-10-15 01:20:28 +00:00
{
/*
1998-10-22 01:33:20 +00:00
* Add 't1' to 't2', storing the result in 't3'.
1998-10-15 01:20:28 +00:00
*/
1998-10-22 01:33:20 +00:00
REQUIRE(t1 != NULL && t2 != NULL && t3 != NULL);
1998-10-15 01:20:28 +00:00
1998-10-22 01:33:20 +00:00
t3->seconds = t1->seconds + t2->seconds;
t3->nanoseconds = t1->nanoseconds + t2->nanoseconds;
if (t3->nanoseconds > 1000000000) {
t3->seconds++;
t3->nanoseconds -= 1000000000;
1998-10-15 01:20:28 +00:00
}
}
void
1998-10-22 01:33:20 +00:00
isc_time_subtract(isc_time_t t1, isc_time_t t2, isc_time_t t3) {
1998-10-15 01:20:28 +00:00
/*
1998-10-22 01:33:20 +00:00
* Subtract 't2' from 't1', storing the result in 't1'.
1998-10-15 01:20:28 +00:00
*/
1998-10-22 01:33:20 +00:00
REQUIRE(t1 != NULL && t2 != NULL && t3 != NULL);
REQUIRE(isc_time_compare(t1, t2) >= 0);
1998-10-15 01:20:28 +00:00
1998-10-22 01:33:20 +00:00
t3->seconds = t1->seconds - t2->seconds;
if (t1->nanoseconds >= t2->nanoseconds)
t3->nanoseconds = t1->nanoseconds - t2->nanoseconds;
1998-10-15 01:20:28 +00:00
else {
1998-10-22 01:33:20 +00:00
t3->nanoseconds = 1000000000 - t2->nanoseconds +
t1->nanoseconds;
t3->seconds--;
1998-10-15 01:20:28 +00:00
}
}