|
|
|
@ -1,12 +1,21 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
#include "timex.h"
|
|
|
|
|
|
|
|
|
|
#define SEC_IN_USEC 1000000
|
|
|
|
|
|
|
|
|
|
timex_t timex_add(const timex_t a, const timex_t b)
|
|
|
|
|
{
|
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
|
if (!timex_isnormalized(a) || !timex_isnormalized(b)) {
|
|
|
|
|
puts("timex_add on denormalized value");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
timex_t result;
|
|
|
|
|
result.seconds = a.seconds + b.seconds;
|
|
|
|
|
result.microseconds = a.microseconds + b.microseconds;
|
|
|
|
@ -29,17 +38,34 @@ void timex_normalize(timex_t *time)
|
|
|
|
|
time->microseconds %= SEC_IN_USEC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int timex_isnormalized(timex_t *time)
|
|
|
|
|
{
|
|
|
|
|
return (time->microseconds < SEC_IN_USEC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timex_t timex_set(uint32_t seconds, uint32_t microseconds)
|
|
|
|
|
{
|
|
|
|
|
timex_t result;
|
|
|
|
|
result.seconds = seconds;
|
|
|
|
|
result.microseconds = microseconds;
|
|
|
|
|
|
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
|
if (!timex_isnormalized(result)) {
|
|
|
|
|
puts("timex_set on denormalized value");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timex_t timex_sub(const timex_t a, const timex_t b)
|
|
|
|
|
{
|
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
|
if (!timex_isnormalized(a) || !timex_isnormalized(b)) {
|
|
|
|
|
puts("timex_sub on denormalized value");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
timex_t result;
|
|
|
|
|
result.seconds = a.seconds - b.seconds;
|
|
|
|
|
result.microseconds = a.microseconds - b.microseconds;
|
|
|
|
@ -49,6 +75,12 @@ timex_t timex_sub(const timex_t a, const timex_t b)
|
|
|
|
|
|
|
|
|
|
int timex_cmp(const timex_t a, const timex_t b)
|
|
|
|
|
{
|
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
|
if (!timex_isnormalized(a) || !timex_isnormalized(b)) {
|
|
|
|
|
puts("timex_cmp on denormalized value");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (a.seconds < b.seconds) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|