timehelper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: timehelper.h
  4. // Created: 6/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2017 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. #include <chrono>
  34. namespace log4cplus {
  35. namespace helpers {
  36. using std::time_t;
  37. using std::tm;
  38. namespace chrono = std::chrono;
  39. typedef chrono::system_clock Clock;
  40. typedef chrono::duration<long long, std::micro> Duration;
  41. typedef chrono::time_point<Clock, Duration> Time;
  42. template <typename FromDuration>
  43. inline
  44. Time
  45. time_cast (chrono::time_point<Clock, FromDuration> const & tp)
  46. {
  47. return chrono::time_point_cast<Duration, Clock> (tp);
  48. }
  49. inline
  50. Time
  51. now ()
  52. {
  53. return time_cast (Clock::now ());
  54. }
  55. inline
  56. Time
  57. from_time_t (time_t t_time)
  58. {
  59. return time_cast (Clock::from_time_t (t_time));
  60. }
  61. inline
  62. time_t
  63. to_time_t (Time const & the_time)
  64. {
  65. // This is based on <http://stackoverflow.com/a/17395137/341065>. It is
  66. // possible that to_time_t() returns rounded time and we want truncation.
  67. time_t time = Clock::to_time_t (the_time);
  68. auto const rounded_time = from_time_t (time);
  69. if (rounded_time > the_time)
  70. --time;
  71. return time;
  72. }
  73. LOG4CPLUS_EXPORT Time from_struct_tm (tm * t);
  74. inline
  75. Time
  76. truncate_fractions (Time const & the_time)
  77. {
  78. return from_time_t (to_time_t (the_time));
  79. }
  80. inline
  81. long
  82. microseconds_part (Time const & the_time)
  83. {
  84. static_assert ((std::ratio_equal<Duration::period, std::micro>::value),
  85. "microseconds");
  86. // This is based on <http://stackoverflow.com/a/17395137/341065>
  87. return static_cast<long>(
  88. (the_time - from_time_t (to_time_t (the_time))).count ());
  89. }
  90. inline
  91. Time
  92. time_from_parts (time_t tv_sec, long tv_usec)
  93. {
  94. return from_time_t (tv_sec) + chrono::microseconds (tv_usec);
  95. }
  96. /**
  97. * Populates <code>tm</code> using the <code>gmtime()</code>
  98. * function.
  99. */
  100. LOG4CPLUS_EXPORT
  101. void gmTime (tm* t, Time const &);
  102. /**
  103. * Populates <code>tm</code> using the <code>localtime()</code>
  104. * function.
  105. */
  106. LOG4CPLUS_EXPORT
  107. void localTime (tm* t, Time const &);
  108. /**
  109. * Returns a string with a "formatted time" specified by
  110. * <code>fmt</code>. It used the <code>strftime()</code>
  111. * function to do this.
  112. *
  113. * Look at your platform's <code>strftime()</code> documentation
  114. * for the formatting options available.
  115. *
  116. * The following additional options are provided:<br>
  117. * <code>%q</code> - 3 character field that provides milliseconds
  118. * <code>%Q</code> - 7 character field that provides fractional
  119. * milliseconds.
  120. */
  121. LOG4CPLUS_EXPORT
  122. log4cplus::tstring getFormattedTime (log4cplus::tstring const & fmt,
  123. Time const & the_time, bool use_gmtime = false);
  124. } // namespace helpers
  125. } // namespace log4cplus
  126. #endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_