_clock_gettimeofday.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*! ========================================================================
  2. ** Extended Template and Library
  3. ** gettimeofday() Clock Description Implementation
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. **
  7. ** This package is free software; you can redistribute it and/or
  8. ** modify it under the terms of the GNU General Public License as
  9. ** published by the Free Software Foundation; either version 2 of
  10. ** the License, or (at your option) any later version.
  11. **
  12. ** This package is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ** General Public License for more details.
  16. **
  17. ** === N O T E S ===========================================================
  18. **
  19. ** This is an internal header file, included by other ETL headers.
  20. ** You should not attempt to use it directly.
  21. **
  22. ** ========================================================================= */
  23. #ifndef __ETL__CLOCK_GETTIMEOFDAY_H
  24. #define __ETL__CLOCK_GETTIMEOFDAY_H
  25. #include <sys/time.h>
  26. #include <cmath>
  27. _ETL_BEGIN_NAMESPACE
  28. class clock_desc_gettimeofday
  29. {
  30. public:
  31. typedef double value_type;
  32. inline static bool realtime()
  33. {
  34. return true;
  35. }
  36. inline static bool proctime()
  37. {
  38. return false;
  39. }
  40. inline static value_type
  41. one_second()
  42. {
  43. return 1.0f;
  44. }
  45. inline static value_type precision()
  46. {
  47. return one_second() / (value_type)1000000.0f;
  48. }
  49. inline static const char *description()
  50. {
  51. return "UNIX gettimeofday()";
  52. };
  53. protected:
  54. class timestamp : public timeval
  55. {
  56. timestamp(int sec, int usec)
  57. {
  58. tv_sec = sec;
  59. tv_usec = usec;
  60. }
  61. friend class clock_desc_gettimeofday;
  62. public:
  63. timestamp() { }
  64. inline timestamp operator-(const timestamp &rhs)const
  65. {
  66. timestamp ret;
  67. ret.tv_usec = tv_usec - rhs.tv_usec;
  68. if (ret.tv_usec < 0) {
  69. ret.tv_sec = tv_sec - rhs.tv_sec - 1;
  70. ret.tv_usec += 1000000;
  71. } else {
  72. ret.tv_sec = tv_sec - rhs.tv_sec;
  73. }
  74. return ret;
  75. }
  76. inline timestamp operator+(timestamp rhs)const
  77. {
  78. rhs.tv_usec += tv_usec;
  79. if (rhs.tv_usec > 1000000) {
  80. rhs.tv_sec += tv_sec + 1;
  81. rhs.tv_usec -= 1000000;
  82. } else {
  83. rhs.tv_sec += tv_sec;
  84. }
  85. return rhs;
  86. }
  87. inline bool operator<(const timestamp &rhs)const
  88. {
  89. return tv_sec < rhs.tv_sec || tv_usec < rhs.tv_usec;
  90. }
  91. inline bool operator==(const timestamp &rhs)const
  92. {
  93. return tv_usec == rhs.tv_usec && tv_sec == rhs.tv_sec;
  94. }
  95. inline bool operator!=(const timestamp &rhs)const
  96. {
  97. return tv_usec != rhs.tv_usec || tv_sec != rhs.tv_sec;
  98. }
  99. };
  100. static void
  101. get_current_time(timestamp &x)
  102. {
  103. gettimeofday(&x, NULL);
  104. }
  105. static timestamp
  106. get_current_time()
  107. {
  108. timestamp ret;
  109. get_current_time(ret);
  110. return ret;
  111. }
  112. static value_type
  113. timestamp_to_seconds(const timestamp &x)
  114. {
  115. return (value_type)x.tv_sec + precision() * x.tv_usec;
  116. }
  117. static timestamp
  118. seconds_to_timestamp(const value_type &x)
  119. {
  120. return timestamp((int)floor(x), (int)((x - floor(x)) / precision() + 0.5));
  121. }
  122. };
  123. _ETL_END_NAMESPACE
  124. #endif