timediff.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "timediff.h"
  25. #include <limits.h>
  26. /*
  27. * Converts number of milliseconds into a timeval structure.
  28. *
  29. * Return values:
  30. * NULL IF tv is NULL or ms < 0 (eg. no timeout -> blocking select)
  31. * tv with 0 in both fields IF ms == 0 (eg. 0ms timeout -> polling select)
  32. * tv with converted fields IF ms > 0 (eg. >0ms timeout -> waiting select)
  33. */
  34. struct timeval *curlx_mstotv(struct timeval *tv, timediff_t ms)
  35. {
  36. if(!tv)
  37. return NULL;
  38. if(ms < 0)
  39. return NULL;
  40. if(ms > 0) {
  41. timediff_t tv_sec = ms / 1000;
  42. timediff_t tv_usec = (ms % 1000) * 1000; /* max=999999 */
  43. #ifdef HAVE_SUSECONDS_T
  44. #if TIMEDIFF_T_MAX > TIME_T_MAX
  45. /* tv_sec overflow check in case time_t is signed */
  46. if(tv_sec > TIME_T_MAX)
  47. tv_sec = TIME_T_MAX;
  48. #endif
  49. tv->tv_sec = (time_t)tv_sec;
  50. tv->tv_usec = (suseconds_t)tv_usec;
  51. #elif defined(_WIN32) /* maybe also others in the future */
  52. #if TIMEDIFF_T_MAX > LONG_MAX
  53. /* tv_sec overflow check on Windows there we know it is long */
  54. if(tv_sec > LONG_MAX)
  55. tv_sec = LONG_MAX;
  56. #endif
  57. tv->tv_sec = (long)tv_sec;
  58. tv->tv_usec = (long)tv_usec;
  59. #else
  60. #if TIMEDIFF_T_MAX > INT_MAX
  61. /* tv_sec overflow check in case time_t is signed */
  62. if(tv_sec > INT_MAX)
  63. tv_sec = INT_MAX;
  64. #endif
  65. tv->tv_sec = (int)tv_sec;
  66. tv->tv_usec = (int)tv_usec;
  67. #endif
  68. }
  69. else {
  70. tv->tv_sec = 0;
  71. tv->tv_usec = 0;
  72. }
  73. return tv;
  74. }
  75. /*
  76. * Converts a timeval structure into number of milliseconds.
  77. */
  78. timediff_t curlx_tvtoms(struct timeval *tv)
  79. {
  80. return (tv->tv_sec*1000) + (timediff_t)(((double)tv->tv_usec)/1000.0);
  81. }