/* IRCfs - IRC FileServ for *nix. * Copyright (C) 2002 Nick 'Zaf' Clifford * For licensing details, refer to the LICENSE file in the source * code directory. */ #include "timeutil.h" #include "runtime.h" /* add_time(a,b,c) * a = b + c * Returns a.tv_sec */ time_t add_time(struct timeval *a, struct timeval *b, struct timeval *c) { time_t usec; ASSERT(a); ASSERT(b); ASSERT(c); usec = b->tv_usec + c->tv_usec; a->tv_sec = b->tv_sec + c->tv_sec + usec / 1000000; a->tv_usec = usec % 1000000; return a->tv_sec; } /* sub_time(a,b,c) * a = b - c * Returns a.tv_sec * Does not return negative numbers. (Will round negative numbers to 0) */ time_t sub_time(struct timeval *a, struct timeval *b, struct timeval *c) { ASSERT(a); ASSERT(b); ASSERT(c); // e = (c > a) ? 0 : (a-c); // a.b - c.d = (d>b) ? (e)-1.(10-d-b) : (e).(b-d) if (c->tv_sec > b->tv_sec) a->tv_sec = 0; else a->tv_sec = b->tv_sec - c->tv_sec; if (c->tv_usec > b->tv_usec) { if (a->tv_sec) { a->tv_sec--; // Decrement a, if not zero a->tv_usec = 1000000 - (c->tv_usec - b->tv_usec); } else a->tv_usec = 0; } else { a->tv_usec = b->tv_usec - c->tv_usec; } return a->tv_sec; } /* time_comp(a,b) * If a < b, returns -1 * if a > b, returns 1 * if a == b, returns 0 */ int time_comp(struct timeval *a, struct timeval *b) { ASSERT(a); ASSERT(b); if (a->tv_sec < b->tv_sec) return -1; if (a->tv_sec > b->tv_sec) return 1; /* a->tv_sec == b->tv_sec */ if (a->tv_usec < b->tv_usec) return -1; if (a->tv_usec > b->tv_usec) return 1; /* a->tv_usec == b->tv_usec */ return 0; }