log4cplus
2.0.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: timehelper.h 00004 // Created: 6/2003 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2003-2015 Tad E. Smith 00009 // 00010 // Licensed under the Apache License, Version 2.0 (the "License"); 00011 // you may not use this file except in compliance with the License. 00012 // You may obtain a copy of the License at 00013 // 00014 // http://www.apache.org/licenses/LICENSE-2.0 00015 // 00016 // Unless required by applicable law or agreed to in writing, software 00017 // distributed under the License is distributed on an "AS IS" BASIS, 00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 // See the License for the specific language governing permissions and 00020 // limitations under the License. 00021 00024 #ifndef LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ 00025 #define LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ 00026 00027 #include <log4cplus/config.hxx> 00028 00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) 00030 #pragma once 00031 #endif 00032 00033 #include <log4cplus/tstring.h> 00034 00035 #if defined (LOG4CPLUS_HAVE_TIME_H) 00036 #include <time.h> 00037 #endif 00038 00039 #include <ctime> 00040 #include <chrono> 00041 00042 00043 namespace log4cplus { 00044 00045 namespace helpers { 00046 00047 00048 using std::time_t; 00049 using std::tm; 00050 namespace chrono = std::chrono; 00051 00052 typedef chrono::system_clock Clock; 00053 typedef chrono::duration<long long, std::micro> Duration; 00054 typedef chrono::time_point<Clock, Duration> Time; 00055 00056 00057 template <typename FromDuration> 00058 inline 00059 Time 00060 time_cast (chrono::time_point<Clock, FromDuration> const & tp) 00061 { 00062 return chrono::time_point_cast<Duration, Clock> (tp); 00063 } 00064 00065 00066 inline 00067 Time 00068 now () 00069 { 00070 return time_cast (Clock::now ()); 00071 } 00072 00073 00074 inline 00075 Time 00076 from_time_t (time_t t_time) 00077 { 00078 return time_cast (Clock::from_time_t (t_time)); 00079 } 00080 00081 00082 inline 00083 time_t 00084 to_time_t (Time const & the_time) 00085 { 00086 // This is based on <http://stackoverflow.com/a/17395137/341065> It is 00087 // possible that to_time_t() returns rounded time and we want truncation. 00088 00089 time_t time = Clock::to_time_t (the_time); 00090 auto const rounded_time = from_time_t (time); 00091 if (rounded_time > the_time) 00092 --time; 00093 00094 return time; 00095 } 00096 00097 00098 LOG4CPLUS_EXPORT Time from_struct_tm (tm * t); 00099 00100 00101 inline 00102 Time 00103 truncate_fractions (Time const & the_time) 00104 { 00105 return from_time_t (to_time_t (the_time)); 00106 } 00107 00108 00109 inline 00110 long 00111 microseconds_part (Time const & the_time) 00112 { 00113 static_assert (std::ratio_equal<Duration::period, std::micro>::value, 00114 "microseconds"); 00115 00116 // This is based on <http://stackoverflow.com/a/17395137/341065> 00117 return static_cast<long>( 00118 (the_time - from_time_t (to_time_t (the_time))).count ()); 00119 } 00120 00121 00122 inline 00123 Time 00124 time_from_parts (time_t tv_sec, long tv_usec) 00125 { 00126 return from_time_t (tv_sec) + chrono::microseconds (tv_usec); 00127 } 00128 00129 00135 LOG4CPLUS_EXPORT 00136 void gmTime (tm* t, Time const &); 00137 00143 LOG4CPLUS_EXPORT 00144 void localTime (tm* t, Time const &); 00145 00159 LOG4CPLUS_EXPORT 00160 log4cplus::tstring getFormattedTime (log4cplus::tstring const & fmt, 00161 Time const & the_time, bool use_gmtime = false); 00162 00163 00164 } // namespace helpers 00165 00166 } // namespace log4cplus 00167 00168 00169 #endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_