dbghlp.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "dbghlp.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. uint64_t Timeval2Us(const struct timeval *ptv)
  8. {
  9. if(ptv)
  10. return (uint64_t)ptv->tv_sec * 1000000 + (uint64_t)ptv->tv_usec;
  11. return (uint64_t)-1;
  12. }
  13. /////////////////////////////////////////////////////////////////////////////
  14. const struct timeval* Us2Timeval(uint64_t usTime, struct timeval *ptv)
  15. {
  16. if(ptv)
  17. {
  18. ptv->tv_sec = usTime / 1000000;
  19. ptv->tv_usec = usTime % 1000000;
  20. }
  21. return ptv;
  22. }
  23. /////////////////////////////////////////////////////////////////////////////
  24. uint64_t Timespec2Ns(const struct timespec *pts)
  25. {
  26. if(pts)
  27. return (uint64_t)pts->tv_sec * 1000000000 + (uint64_t)pts->tv_nsec;
  28. return (uint64_t)-1;
  29. }
  30. /////////////////////////////////////////////////////////////////////////////
  31. const struct timespec* Ns2Timespec(uint64_t nsTime, struct timespec *pts)
  32. {
  33. if(pts)
  34. {
  35. pts->tv_sec = nsTime / 1000000000;
  36. pts->tv_nsec = nsTime % 1000000000;
  37. }
  38. return pts;
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. int64_t TimespecDiff(const struct timespec *pts1, const struct timespec *pts2)
  42. {
  43. int64_t nRet = 0;
  44. if(pts1 && pts2)
  45. {
  46. uint64_t t1 = Timespec2Ns(pts1);
  47. uint64_t t2 = Timespec2Ns(pts2);
  48. nRet = (int64_t)(t1 - t2);
  49. }
  50. return nRet;
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. void GetTimespec(struct timespec *pts)
  54. {
  55. clock_gettime(CLOCK_MONOTONIC, pts);
  56. }