Home | History | Annotate | Download | only in libiberty
      1 /* Basic struct timeval utilities.
      2    Copyright (C) 2011 Free Software Foundation, Inc.
      3 
      4 This file is part of the libiberty library.
      5 Libiberty is free software; you can redistribute it and/or
      6 modify it under the terms of the GNU Library General Public
      7 License as published by the Free Software Foundation; either
      8 version 2 of the License, or (at your option) any later version.
      9 
     10 Libiberty is distributed in the hope that it will be useful,
     11 but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13 Library General Public License for more details.
     14 
     15 You should have received a copy of the GNU Library General Public
     16 License along with libiberty; see the file COPYING.LIB.  If not,
     17 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
     18 Boston, MA 02110-1301, USA.  */
     19 
     20 #include "config.h"
     21 
     22 /* On some systems (such as WindISS), you must include <sys/types.h>
     23    to get the definition of "time_t" before you include <time.h>.  */
     24 #include <sys/types.h>
     25 
     26 #ifdef TIME_WITH_SYS_TIME
     27 # include <sys/time.h>
     28 # include <time.h>
     29 #else
     30 # if HAVE_SYS_TIME_H
     31 #  include <sys/time.h>
     32 # else
     33 #  ifdef HAVE_TIME_H
     34 #   include <time.h>
     35 #  endif
     36 # endif
     37 #endif
     38 
     39 #include "timeval-utils.h"
     40 
     41 /*
     42 
     43 @deftypefn Extension void timeval_add (struct timeval *@var{a}, @
     44   struct timeval *@var{b}, struct timeval *@var{result})
     45 
     46 Adds @var{a} to @var{b} and stores the result in @var{result}.
     47 
     48 @end deftypefn
     49 
     50 */
     51 
     52 void
     53 timeval_add (struct timeval *result,
     54 	     const struct timeval *a, const struct timeval *b)
     55 {
     56   result->tv_sec = a->tv_sec + b->tv_sec;
     57   result->tv_usec = a->tv_usec + b->tv_usec;
     58   if (result->tv_usec >= 1000000)
     59     {
     60       ++result->tv_sec;
     61       result->tv_usec -= 1000000;
     62     }
     63 }
     64 
     65 /*
     66 
     67 @deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
     68   struct timeval *@var{b}, struct timeval *@var{result})
     69 
     70 Subtracts @var{b} from @var{a} and stores the result in @var{result}.
     71 
     72 @end deftypefn
     73 
     74 */
     75 
     76 void
     77 timeval_sub (struct timeval *result,
     78 	     const struct timeval *a, const struct timeval *b)
     79 {
     80   result->tv_sec = a->tv_sec - b->tv_sec;
     81   result->tv_usec = a->tv_usec - b->tv_usec;
     82   if (result->tv_usec < 0)
     83     {
     84       --result->tv_sec;
     85       result->tv_usec += 1000000;
     86     }
     87 }
     88