2 * tod.c -- time of day pseudo-class implementation
4 * Copyright (c) 1995 John H. Poplett
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Absolutely no warranty of function or purpose is made by the author
18 * 4. This work was done expressly for inclusion into FreeBSD. Other use
19 * is allowed if this notation is included.
20 * 5. Modifications may be freely made to this file if the above conditions
25 #include <sys/types.h>
35 int tod_cmp (const struct timeval *a, const struct timeval *b)
38 assert (a->tv_usec <= USP);
39 assert (b->tv_usec <= USP);
40 rc = a->tv_sec - b->tv_sec;
42 rc = a->tv_usec - b->tv_usec;
49 int tod_lt (const struct timeval *a, const struct timeval *b)
51 return tod_cmp (a, b) < 0;
54 int tod_gt (const struct timeval *a, const struct timeval *b)
56 return tod_cmp (a, b) > 0;
59 int tod_lte (const struct timeval *a, const struct timeval *b)
61 return tod_cmp (a, b) <= 0;
64 int tod_gte (const struct timeval *a, const struct timeval *b)
66 return tod_cmp (a, b) >= 0;
69 int tod_eq (const struct timeval *a, const struct timeval *b)
71 return tod_cmp (a, b) == 0;
77 void tod_addto (struct timeval *a, const struct timeval *b)
79 a->tv_usec += b->tv_usec;
80 a->tv_sec += b->tv_sec + a->tv_usec / USP;
87 void tod_subfrom (struct timeval *a, struct timeval b)
89 assert (a->tv_usec <= USP);
90 assert (b.tv_usec <= USP);
91 if (b.tv_usec > a->tv_usec)
96 a->tv_usec -= b.tv_usec;
97 a->tv_sec -= b.tv_sec;
100 void tod_gettime (struct timeval *tp)
102 gettimeofday (tp, NULL);
103 tp->tv_sec += tp->tv_usec / USP;