2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-25 19:47:42 +00:00
bind/lib/isc/win32/time.c

171 lines
3.4 KiB
C
Raw Normal View History

1998-10-23 05:45:44 +00:00
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
1998-10-23 06:02:07 +00:00
#include <windows.h>
1998-10-23 05:45:44 +00:00
#include <isc/assertions.h>
#include <isc/time.h>
1998-10-26 23:07:57 +00:00
/***
*** Intervals
***/
void
isc_interval_set(isc_interval_t i,
unsigned int seconds, unsigned int nanoseconds) {
/*
* Set 'i' to a value representing an interval of 'seconds' seconds
* and 'nanoseconds' nanoseconds, suitable for use in isc_time_add()
* and isc_time_subtract().
*/
REQUIRE(i != NULL);
REQUIRE(nanoseconds < 1000000000);
i->interval = (LONGLONG)seconds * 10000000 + nanoseconds / 100;
}
isc_boolean_t
isc_interval_iszero(isc_interval_t i) {
/*
* Returns ISC_TRUE iff. 'i' is the zero interval.
*/
REQUIRE(i != NULL);
if (i->interval == 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
/***
*** Absolute Times
***/
1998-10-27 03:12:07 +00:00
static FILETIME epoch = { 0, 0 };
1998-10-26 23:07:57 +00:00
void
isc_time_settoepoch(isc_time_t t) {
/*
* Set 't' to the time of the epoch.
*/
REQUIRE(t != NULL);
1998-10-27 03:12:07 +00:00
t->absolute = epoch;
1998-10-26 23:07:57 +00:00
}
isc_boolean_t
isc_time_isepoch(isc_time_t t) {
/*
* Returns ISC_TRUE iff. 't' is the epoch ("time zero").
*/
REQUIRE(t != NULL);
1998-10-27 03:12:07 +00:00
if (CompareFileTime(&t->absolute, &epoch) == 0)
1998-10-26 23:07:57 +00:00
return (ISC_TRUE);
return (ISC_FALSE);
}
1998-10-23 05:45:44 +00:00
isc_result
isc_time_get(isc_time_t t) {
/*
1998-10-26 23:07:57 +00:00
* Set *t to the current absolute time.
1998-10-23 05:45:44 +00:00
*/
REQUIRE(t != NULL);
1998-10-23 06:02:07 +00:00
1998-10-27 03:12:07 +00:00
GetSystemTimeAsFileTime(&t->absolute);
1998-10-23 06:02:07 +00:00
1998-10-23 05:45:44 +00:00
return (ISC_R_SUCCESS);
}
int
isc_time_compare(isc_time_t t1, isc_time_t t2) {
1998-10-27 03:12:07 +00:00
LARGE_INTEGER i1, i2;
1998-10-26 23:07:57 +00:00
1998-10-23 05:45:44 +00:00
/*
* Compare the times referenced by 't1' and 't2'
*/
REQUIRE(t1 != NULL && t2 != NULL);
1998-10-27 03:12:07 +00:00
return ((int)CompareFileTime(&t1->absolute, &t2->absolute));
1998-10-23 05:45:44 +00:00
}
void
1998-10-26 23:07:57 +00:00
isc_time_add(isc_time_t t, isc_interval_t i, isc_time_t result)
1998-10-23 05:45:44 +00:00
{
1998-10-27 03:12:07 +00:00
LARGE_INTEGER i1, i2;
1998-10-23 05:45:44 +00:00
/*
1998-10-26 23:07:57 +00:00
* Add 't' to 'i', storing the result in 'result'.
1998-10-23 05:45:44 +00:00
*/
1998-10-26 23:07:57 +00:00
REQUIRE(t != NULL && i != NULL && result != NULL);
1998-10-23 05:45:44 +00:00
1998-10-27 03:12:07 +00:00
i1.LowPart = t->absolute.dwLowDateTime;
/* XXX trouble here if high bit set (i.e. signed -> unsigned?) */
i1.HighPart = t->absolute.dwHighDateTime;
i2.QuadPart = i1.QuadPart + i.interval;
/* XXX potential signed to unsigned problem */
result->absolute.dwLowDateTime = i2.LowPart;
result->absolute.dwHighDateTime = i2.HighPart;
1998-10-23 05:45:44 +00:00
}
void
1998-10-26 23:07:57 +00:00
isc_time_subtract(isc_time_t t, isc_interval_t i, isc_time_t result) {
1998-10-27 03:12:07 +00:00
LARGE_INTEGER i1, i2;
1998-10-23 05:45:44 +00:00
/*
1998-10-26 23:07:57 +00:00
* Subtract 'i' from 't', storing the result in 'result'.
1998-10-23 05:45:44 +00:00
*/
1998-10-26 23:07:57 +00:00
REQUIRE(t != NULL && i != NULL && result != NULL);
1998-10-27 03:12:07 +00:00
i1.LowPart = t->absolute.dwLowDateTime;
/* XXX trouble here if high bit set (i.e. signed -> unsigned?) */
i1.HighPart = t->absolute.dwHighDateTime;
i2.QuadPart = i1.QuadPart - i.interval;
1998-10-23 05:45:44 +00:00
1998-10-27 03:12:07 +00:00
/* XXX potential signed to unsigned problem */
result->absolute.dwLowDateTime = i2.LowPart;
result->absolute.dwHighDateTime = i2.HighPart;
1998-10-23 05:45:44 +00:00
}
1998-10-26 23:07:57 +00:00
/***
1998-10-27 03:12:07 +00:00
*** Win32 Only
1998-10-26 23:07:57 +00:00
***/
1998-10-27 03:12:07 +00:00
unsigned int
isc_time_millidiff(isc_time_t t1, isc_time_t t2) {
LARGE_INTEGER i1, i2;
LONGLONG i3;
i1.LowPart = t1->absolute.dwLowDateTime;
/* XXX trouble here if high bit set (i.e. signed -> unsigned?) */
i1.HighPart = t1->absolute.dwHighDateTime;
i2.LowPart = t2->absolute.dwLowDateTime;
/* XXX trouble here if high bit set (i.e. signed -> unsigned?) */
i2.HighPart = t2->absolute.dwHighDateTime;
if (i1.QuadPart <= i2.QuadPart)
return (0);
i3 = (i1.QuadPart - i2.QuadPart) / 10000; /* convert to milliseconds */
if (i3 > 1000000000) /* XXX arbitrary! */
return (1000000000);
return ((unsigned int)(i3));
1998-10-26 23:07:57 +00:00
}