| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: tv.c,v 1.1 1998/11/25 23:30:01 mdw Exp $ |
| 4 | * |
| 5 | * Manipulation of timeval structures |
| 6 | * |
| 7 | * (c) 1998 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 14 | * mLib is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * mLib is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with mLib; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: tv.c,v $ |
| 32 | * Revision 1.1 1998/11/25 23:30:01 mdw |
| 33 | * New file. |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #include <sys/time.h> |
| 40 | #include "tv.h" |
| 41 | |
| 42 | /*----- Main code ---------------------------------------------------------*/ |
| 43 | |
| 44 | /* --- @tv_add@ --- * |
| 45 | * |
| 46 | * Arguments: @struct timeval *dst@ = destination block |
| 47 | * @const struct timeval *a, *b@ = source blocks |
| 48 | * |
| 49 | * Returns: --- |
| 50 | * |
| 51 | * Use: Adds two timevals. |
| 52 | */ |
| 53 | |
| 54 | void tv_add(struct timeval *dst, |
| 55 | const struct timeval *a, const struct timeval *b) |
| 56 | { |
| 57 | dst->tv_sec = a->tv_sec + b->tv_sec; |
| 58 | dst->tv_usec = a->tv_usec + b->tv_usec; |
| 59 | if (dst->tv_usec >= 1000000) { |
| 60 | dst->tv_usec -= 1000000; |
| 61 | dst->tv_sec++; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* --- @tv_sub@ --- * |
| 66 | * |
| 67 | * Arguments: @struct timeval *dst@ = destination block |
| 68 | * @const struct timeval *a, *b@ = source blocks |
| 69 | * |
| 70 | * Returns: --- |
| 71 | * |
| 72 | * Use: Subtracts two timevals. |
| 73 | */ |
| 74 | |
| 75 | void tv_sub(struct timeval *dst, |
| 76 | const struct timeval *a, const struct timeval *b) |
| 77 | { |
| 78 | dst->tv_sec = a->tv_sec - b->tv_sec; |
| 79 | if (a->tv_usec >= b->tv_usec) |
| 80 | dst->tv_usec = a->tv_usec - b->tv_usec; |
| 81 | else { |
| 82 | dst->tv_usec = a->tv_usec + 1000000 - b->tv_usec; |
| 83 | dst->tv_sec--; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* --- @tv_cmp@ --- * |
| 88 | * |
| 89 | * Arguments: @const struct timeval *a, *b@ = source blocks |
| 90 | * |
| 91 | * Returns: Less than, equal to, or greater than zero. |
| 92 | * |
| 93 | * Use: Compares two timevals. |
| 94 | */ |
| 95 | |
| 96 | int tv_cmp(const struct timeval *a, const struct timeval *b) |
| 97 | { |
| 98 | if (a->tv_sec > b->tv_sec) |
| 99 | return (1); |
| 100 | else if (a->tv_sec < b->tv_sec) |
| 101 | return (-1); |
| 102 | else if (a->tv_usec > b->tv_usec) |
| 103 | return (1); |
| 104 | else if (a->tv_usec < b->tv_usec) |
| 105 | return (-1); |
| 106 | else |
| 107 | return (0); |
| 108 | } |
| 109 | |
| 110 | /*----- That's all, folks -------------------------------------------------*/ |