You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.1KB

  1. // ---
  2. //
  3. // $Id: time.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $
  4. //
  5. // CppTest - A C++ Unit Testing Framework
  6. // Copyright (c) 2003 Niklas Lundell
  7. //
  8. // ---
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the
  22. // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. // Boston, MA 02111-1307, USA.
  24. //
  25. // ---
  26. #include "missing.h"
  27. #include "cpptest-time.h"
  28. namespace Test
  29. {
  30. namespace
  31. {
  32. const unsigned int UsecPerSec = 1000000;
  33. } // anonymous namespace
  34. /// Constructs a time object with zeroed time.
  35. ///
  36. Time::Time()
  37. : _sec(0),
  38. _usec(0)
  39. {}
  40. /// Constructs a time object.
  41. ///
  42. /// \param sec Seconds.
  43. /// \param usec Micro-seconds.
  44. ///
  45. Time::Time(unsigned int sec, unsigned int usec)
  46. : _sec(sec),
  47. _usec(usec)
  48. {}
  49. /// \return Seconds.
  50. ///
  51. unsigned int
  52. Time::seconds() const
  53. {
  54. return _sec;
  55. }
  56. /// \return Micro-seconds.
  57. ///
  58. unsigned int
  59. Time::microseconds() const
  60. {
  61. return _usec;
  62. }
  63. /// \return The current time.
  64. ///
  65. Time
  66. Time::current()
  67. {
  68. struct timeval tv;
  69. gettimeofday(&tv, 0);
  70. return Time(tv.tv_sec, tv.tv_usec);
  71. }
  72. /// \relates Time
  73. ///
  74. /// Computes the time elapsed between two time values.
  75. ///
  76. /// \param t1 Left-hand time, should be greater than \a t2.
  77. /// \param t2 Right-hand time, should be less than \a t1.
  78. ///
  79. /// \return Computed time value.
  80. ///
  81. Time
  82. operator-(const Time& t1, const Time& t2)
  83. {
  84. if (t2._sec > t1._sec || (t2._sec == t1._sec && t2._usec > t1._usec))
  85. return Time();
  86. unsigned int sec = t1._sec - t2._sec;
  87. unsigned int usec;
  88. if (t2._usec > t1._usec)
  89. {
  90. --sec;
  91. usec = UsecPerSec - (t2._usec - t1._usec);
  92. }
  93. else
  94. usec = t1._usec - t2._usec;
  95. return Time(sec, usec);
  96. }
  97. /// \relates Time
  98. ///
  99. /// Adds two time values.
  100. ///
  101. /// \param t1 Left-hand time.
  102. /// \param t2 Right-hand time.
  103. ///
  104. /// \return Computed time value.
  105. ///
  106. Time
  107. operator+(const Time& t1, const Time& t2)
  108. {
  109. unsigned int sec = t1._sec + t2._sec;
  110. unsigned int usec = t1._usec + t2._usec;
  111. if (usec > UsecPerSec)
  112. {
  113. ++sec;
  114. usec -= UsecPerSec;
  115. }
  116. return Time(sec, usec);
  117. }
  118. /// \relates Time
  119. ///
  120. /// Outputs a time to an output stream.
  121. ///
  122. /// \param os Output stream to write to.
  123. /// \param t %Time to output.
  124. ///
  125. /// \return A reference to the given output stream.
  126. ///
  127. std::ostream&
  128. operator<<(std::ostream& os, const Time& t)
  129. {
  130. int old_fill(os.fill());
  131. int old_width(os.width());
  132. os << t.seconds() << '.';
  133. os.fill('0');
  134. os.width(6);
  135. os << t.microseconds();
  136. os.fill(old_fill);
  137. os.width(old_width);
  138. return os;
  139. }
  140. } // namespace Test