timeval-utils.c 2.2 KB

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