core_time.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include "core_time.h"
  10. /*
  11. * time in miliseconds
  12. * args:
  13. * none
  14. *
  15. * asserts:
  16. * none
  17. *
  18. * returns: time of day in miliseconds
  19. */
  20. //uint32_t ms_time ()
  21. //{
  22. // struct timeval now;
  23. //
  24. // if(gettimeofday(&now, NULL) != 0)
  25. // {
  26. // fprintf(stderr, "V4L2_CORE: ms_time (gettimeofday) error: %s\n", strerror(errno));
  27. // return 0;
  28. // }
  29. //
  30. // uint32_t mst = (uint32_t) now.tv_sec * 1000 + (uint32_t) now.tv_usec / 1000;
  31. //
  32. // return (mst);
  33. //}
  34. /*
  35. * time in microseconds
  36. * args:
  37. * none
  38. *
  39. * asserts:
  40. * none
  41. *
  42. * returns: time of day in microseconds
  43. */
  44. //uint64_t us_time(void)
  45. //{
  46. // struct timeval now;
  47. //
  48. // if(gettimeofday(&now, NULL) != 0)
  49. // {
  50. // fprintf(stderr, "V4L2_CORE: us_time (gettimeofday) error: %s\n", strerror(errno));
  51. // return 0;
  52. // }
  53. //
  54. // uint64_t ust = (uint64_t) now.tv_sec * USEC_PER_SEC + (uint64_t) now.tv_usec;
  55. //
  56. // return (ust);
  57. //}
  58. /*
  59. * time in nanoseconds
  60. * args:
  61. * none
  62. *
  63. * asserts:
  64. * none
  65. *
  66. * returns: time in nanoseconds
  67. */
  68. //uint64_t ns_time (void)
  69. //{
  70. // struct timespec now;
  71. //
  72. // if(clock_gettime(CLOCK_REALTIME, &now) != 0)
  73. // {
  74. // fprintf(stderr, "V4L2_CORE: ns_time (clock_gettime) error: %s\n", strerror(errno));
  75. // return 0;
  76. // }
  77. //
  78. // return ((uint64_t) now.tv_sec * NSEC_PER_SEC + (uint64_t) now.tv_nsec);
  79. //}
  80. /*
  81. * monotonic time in nanoseconds
  82. * args:
  83. * none
  84. *
  85. * asserts:
  86. * none
  87. *
  88. * returns: monotonic time in nanoseconds
  89. */
  90. uint64_t ns_time_monotonic()
  91. {
  92. struct timespec now;
  93. if(clock_gettime(CLOCK_MONOTONIC, &now) != 0)
  94. {
  95. printf("V4L2_CORE: ns_time_monotonic (clock_gettime) error: %s\n", strerror(errno));
  96. return 0;
  97. }
  98. return ((uint64_t)now.tv_sec * NSEC_PER_SEC + (uint64_t) now.tv_nsec);
  99. }