1 /* 2 * tod.c -- time of day pseudo-class implementation 3 * 4 * Copyright (c) 1995 John H. Poplett 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 17 * John H. Poplett. 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 21 * are met. 22 * 23 */ 24 25 #include <sys/types.h> 26 #include <sys/time.h> 27 28 #include <assert.h> 29 #include <stdio.h> 30 31 #include "tod.h" 32 33 #define USP 1000000 34 35 int tod_cmp (const struct timeval *a, const struct timeval *b) 36 { 37 int rc; 38 assert (a->tv_usec <= USP); 39 assert (b->tv_usec <= USP); 40 rc = a->tv_sec - b->tv_sec; 41 if (rc == 0) 42 rc = a->tv_usec - b->tv_usec; 43 return rc; 44 } 45 46 /* 47 TOD < command 48 */ 49 int tod_lt (const struct timeval *a, const struct timeval *b) 50 { 51 return tod_cmp (a, b) < 0; 52 } 53 54 int tod_gt (const struct timeval *a, const struct timeval *b) 55 { 56 return tod_cmp (a, b) > 0; 57 } 58 59 int tod_lte (const struct timeval *a, const struct timeval *b) 60 { 61 return tod_cmp (a, b) <= 0; 62 } 63 64 int tod_gte (const struct timeval *a, const struct timeval *b) 65 { 66 return tod_cmp (a, b) >= 0; 67 } 68 69 int tod_eq (const struct timeval *a, const struct timeval *b) 70 { 71 return tod_cmp (a, b) == 0; 72 } 73 74 /* 75 TOD += command 76 */ 77 void tod_addto (struct timeval *a, const struct timeval *b) 78 { 79 a->tv_usec += b->tv_usec; 80 a->tv_sec += b->tv_sec + a->tv_usec / USP; 81 a->tv_usec %= USP; 82 } 83 84 /* 85 TOD -= command 86 */ 87 void tod_subfrom (struct timeval *a, struct timeval b) 88 { 89 assert (a->tv_usec <= USP); 90 assert (b.tv_usec <= USP); 91 if (b.tv_usec > a->tv_usec) 92 { 93 a->tv_usec += USP; 94 a->tv_sec -= 1; 95 } 96 a->tv_usec -= b.tv_usec; 97 a->tv_sec -= b.tv_sec; 98 } 99 100 void tod_gettime (struct timeval *tp) 101 { 102 gettimeofday (tp, NULL); 103 tp->tv_sec += tp->tv_usec / USP; 104 tp->tv_usec %= USP; 105 } 106 107 /* end of tod.c */ 108