X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/c846879ccf3e86ea293c157f4aa2ff8716fb5b4c..ff1e93acf2626d2531a97de3b54bce9b5a37bcb5:/tv.h?ds=sidebyside diff --git a/tv.h b/tv.h index 01dd9af..d93e281 100644 --- a/tv.h +++ b/tv.h @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: tv.h,v 1.2 1999/05/05 18:50:31 mdw Exp $ + * $Id: tv.h,v 1.7 2004/04/08 01:36:13 mdw Exp $ * * Manipulation of timeval structures * * (c) 1998 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,30 +15,20 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with mLib; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -/*----- Revision history --------------------------------------------------* - * - * $Log: tv.h,v $ - * Revision 1.2 1999/05/05 18:50:31 mdw - * Change licensing conditions to LGPL. - * - * Revision 1.1 1998/11/25 23:30:01 mdw - * New file. * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. */ -#ifndef TV_H -#define TV_H +#ifndef MLIB_TV_H +#define MLIB_TV_H #ifdef __cplusplus extern "C" { @@ -48,6 +38,10 @@ #include +/*----- A macro to make reading easier ------------------------------------*/ + +#define MILLION 1000000 + /*----- Functions provided ------------------------------------------------*/ /* --- @tv_add@ --- * @@ -64,6 +58,32 @@ extern void tv_add(struct timeval */*dst*/, const struct timeval */*a*/, const struct timeval */*b*/); +#define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec) + +/* --- @tv_addl@ --- * + * + * Arguments: @struct timeval *dst@ = destination block + * @const struct timeval *a@ = source blocks + * @time_t sec@, @unsigned long usec@ = time to add + * + * Returns: --- + * + * Use: Adds a literal time in seconds and microseconds. + */ + +extern void tv_addl(struct timeval */*dst*/, + const struct timeval */*a*/, + time_t /*sec*/, unsigned long /*usec*/); + +#define TV_ADDL(dst, a, sec, usec) do { \ + (dst)->tv_sec = (a)->tv_sec + (sec); \ + (dst)->tv_usec = (a)->tv_usec + (usec); \ + if ((dst)->tv_usec >= MILLION) { \ + (dst)->tv_usec -= MILLION; \ + (dst)->tv_sec++; \ + } \ +} while (0) + /* --- @tv_sub@ --- * * * Arguments: @struct timeval *dst@ = destination block @@ -78,6 +98,33 @@ extern void tv_sub(struct timeval */*dst*/, const struct timeval */*a*/, const struct timeval */*b*/); +#define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec) + +/* --- @tv_subl@ --- * + * + * Arguments: @struct timeval *dst@ = destination block + * @const struct timeval *a@ = source blocks + * @time_t sec@, @unsigned long usec@ = time to subtract + * + * Returns: --- + * + * Use: Subtracts a literal time in seconds and microseconds. + */ + +extern void tv_subl(struct timeval */*dst*/, + const struct timeval */*a*/, + time_t /*sec*/, unsigned long /*usec*/); + +#define TV_SUBL(dst, a, sec, usec) do { \ + (dst)->tv_sec = (a)->tv_sec - (sec); \ + if ((a)->tv_usec >= (usec)) \ + (dst)->tv_usec = (a)->tv_usec - (usec); \ + else { \ + (dst)->tv_usec = (a)->tv_usec + MILLION - (usec); \ + (dst)->tv_sec--; \ + } \ +} while (0) + /* --- @tv_cmp@ --- * * * Arguments: @const struct timeval *a, *b@ = source blocks @@ -90,6 +137,10 @@ extern void tv_sub(struct timeval */*dst*/, extern int tv_cmp(const struct timeval */*a*/, const struct timeval */*b*/); +#define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ? \ + (a)->tv_usec op (b)->tv_usec : \ + (a)->tv_sec op (b)->tv_sec) + /*----- That's all, folks -------------------------------------------------*/ #ifdef __cplusplus