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.

179 lines
5.1 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: timehelper.h
  4. // Created: 6/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2010 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
  23. #define LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/tstring.h>
  29. #if defined (LOG4CPLUS_HAVE_TIME_H)
  30. #include <time.h>
  31. #endif
  32. #include <ctime>
  33. namespace log4cplus {
  34. namespace helpers {
  35. using std::time_t;
  36. using std::tm;
  37. /**
  38. * This class represents a Epoch time with microsecond accuracy.
  39. */
  40. class LOG4CPLUS_EXPORT Time {
  41. public:
  42. Time();
  43. Time(time_t tv_sec, long tv_usec);
  44. explicit Time(time_t time);
  45. /**
  46. * Returns the current time using the <code>gettimeofday()</code>
  47. * method if it is available on the current platform. (Not on
  48. * WIN32.)
  49. */
  50. static Time gettimeofday();
  51. // Methods
  52. /**
  53. * Returns <i>seconds</i> value.
  54. */
  55. time_t sec() const { return tv_sec; }
  56. /**
  57. * Returns <i>microseconds</i> value.
  58. */
  59. long usec() const { return tv_usec; }
  60. /**
  61. * Sets the <i>seconds</i> value.
  62. */
  63. void sec(time_t s) { tv_sec = s; }
  64. /**
  65. * Sets the <i>microseconds</i> value.
  66. */
  67. void usec(long us) { tv_usec = us; }
  68. /**
  69. * Sets this Time using the <code>mktime</code> function.
  70. */
  71. time_t setTime(tm* t);
  72. /**
  73. * Returns this Time as a <code>time_t</code> value.
  74. */
  75. time_t getTime() const LOG4CPLUS_ATTRIBUTE_PURE;
  76. /**
  77. * Populates <code>tm</code> using the <code>gmtime()</code>
  78. * function.
  79. */
  80. void gmtime(tm* t) const;
  81. /**
  82. * Populates <code>tm</code> using the <code>localtime()</code>
  83. * function.
  84. */
  85. void localtime(tm* t) const;
  86. /**
  87. * Returns a string with a "formatted time" specified by
  88. * <code>fmt</code>. It used the <code>strftime()</code>
  89. * function to do this.
  90. *
  91. * Look at your platform's <code>strftime()</code> documentation
  92. * for the formatting options available.
  93. *
  94. * The following additional options are provided:<br>
  95. * <code>%q</code> - 3 character field that provides milliseconds
  96. * <code>%Q</code> - 7 character field that provides fractional
  97. * milliseconds.
  98. */
  99. log4cplus::tstring getFormattedTime(const log4cplus::tstring& fmt,
  100. bool use_gmtime = false) const;
  101. // Operators
  102. Time& operator+=(const Time& rhs);
  103. Time& operator-=(const Time& rhs);
  104. Time& operator/=(long rhs);
  105. Time& operator*=(long rhs);
  106. private:
  107. // Data
  108. time_t tv_sec; /* seconds */
  109. long tv_usec; /* microseconds */
  110. };
  111. LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator+
  112. (const log4cplus::helpers::Time& lhs,
  113. const log4cplus::helpers::Time& rhs);
  114. LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator-
  115. (const log4cplus::helpers::Time& lhs,
  116. const log4cplus::helpers::Time& rhs);
  117. LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator/
  118. (const log4cplus::helpers::Time& lhs,
  119. long rhs);
  120. LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator*
  121. (const log4cplus::helpers::Time& lhs,
  122. long rhs);
  123. LOG4CPLUS_EXPORT bool operator<(const log4cplus::helpers::Time& lhs,
  124. const log4cplus::helpers::Time& rhs)
  125. LOG4CPLUS_ATTRIBUTE_PURE;
  126. LOG4CPLUS_EXPORT bool operator<=(const log4cplus::helpers::Time& lhs,
  127. const log4cplus::helpers::Time& rhs)
  128. LOG4CPLUS_ATTRIBUTE_PURE;
  129. LOG4CPLUS_EXPORT bool operator>(const log4cplus::helpers::Time& lhs,
  130. const log4cplus::helpers::Time& rhs)
  131. LOG4CPLUS_ATTRIBUTE_PURE;
  132. LOG4CPLUS_EXPORT bool operator>=(const log4cplus::helpers::Time& lhs,
  133. const log4cplus::helpers::Time& rhs)
  134. LOG4CPLUS_ATTRIBUTE_PURE;
  135. LOG4CPLUS_EXPORT bool operator==(const log4cplus::helpers::Time& lhs,
  136. const log4cplus::helpers::Time& rhs)
  137. LOG4CPLUS_ATTRIBUTE_PURE;
  138. LOG4CPLUS_EXPORT bool operator!=(const log4cplus::helpers::Time& lhs,
  139. const log4cplus::helpers::Time& rhs)
  140. LOG4CPLUS_ATTRIBUTE_PURE;
  141. } // namespace helpers
  142. } // namespace log4cplus
  143. #endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_