log4cplus
1.1.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: timehelper.h 00004 // Created: 6/2003 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2003-2010 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 00041 00042 namespace log4cplus { 00043 00044 namespace helpers { 00045 00046 00047 using std::time_t; 00048 using std::tm; 00049 00050 00054 class LOG4CPLUS_EXPORT Time { 00055 public: 00056 Time(); 00057 Time(time_t tv_sec, long tv_usec); 00058 explicit Time(time_t time); 00059 00065 static Time gettimeofday(); 00066 00067 // Methods 00071 time_t sec() const { return tv_sec; } 00072 00076 long usec() const { return tv_usec; } 00077 00081 void sec(time_t s) { tv_sec = s; } 00082 00086 void usec(long us) { tv_usec = us; } 00087 00091 time_t setTime(tm* t); 00092 00096 time_t getTime() const LOG4CPLUS_ATTRIBUTE_PURE; 00097 00102 void gmtime(tm* t) const; 00103 00108 void localtime(tm* t) const; 00109 00123 log4cplus::tstring getFormattedTime(const log4cplus::tstring& fmt, 00124 bool use_gmtime = false) const; 00125 00126 // Operators 00127 Time& operator+=(const Time& rhs); 00128 Time& operator-=(const Time& rhs); 00129 Time& operator/=(long rhs); 00130 Time& operator*=(long rhs); 00131 00132 private: 00133 // Data 00134 time_t tv_sec; /* seconds */ 00135 long tv_usec; /* microseconds */ 00136 }; 00137 00138 00139 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator+ 00140 (const log4cplus::helpers::Time& lhs, 00141 const log4cplus::helpers::Time& rhs); 00142 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator- 00143 (const log4cplus::helpers::Time& lhs, 00144 const log4cplus::helpers::Time& rhs); 00145 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator/ 00146 (const log4cplus::helpers::Time& lhs, 00147 long rhs); 00148 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator* 00149 (const log4cplus::helpers::Time& lhs, 00150 long rhs); 00151 00152 LOG4CPLUS_EXPORT bool operator<(const log4cplus::helpers::Time& lhs, 00153 const log4cplus::helpers::Time& rhs) 00154 LOG4CPLUS_ATTRIBUTE_PURE; 00155 LOG4CPLUS_EXPORT bool operator<=(const log4cplus::helpers::Time& lhs, 00156 const log4cplus::helpers::Time& rhs) 00157 LOG4CPLUS_ATTRIBUTE_PURE; 00158 00159 LOG4CPLUS_EXPORT bool operator>(const log4cplus::helpers::Time& lhs, 00160 const log4cplus::helpers::Time& rhs) 00161 LOG4CPLUS_ATTRIBUTE_PURE; 00162 LOG4CPLUS_EXPORT bool operator>=(const log4cplus::helpers::Time& lhs, 00163 const log4cplus::helpers::Time& rhs) 00164 LOG4CPLUS_ATTRIBUTE_PURE; 00165 00166 LOG4CPLUS_EXPORT bool operator==(const log4cplus::helpers::Time& lhs, 00167 const log4cplus::helpers::Time& rhs) 00168 LOG4CPLUS_ATTRIBUTE_PURE; 00169 LOG4CPLUS_EXPORT bool operator!=(const log4cplus::helpers::Time& lhs, 00170 const log4cplus::helpers::Time& rhs) 00171 LOG4CPLUS_ATTRIBUTE_PURE; 00172 00173 } // namespace helpers 00174 00175 } // namespace log4cplus 00176 00177 00178 #endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ 00179