00001 // 00002 // Timestamp.h 00003 // 00004 // $Id: //poco/1.3/Foundation/include/Poco/Timestamp.h#2 $ 00005 // 00006 // Library: Foundation 00007 // Package: DateTime 00008 // Module: Timestamp 00009 // 00010 // Definition of the Timestamp class. 00011 // 00012 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 00013 // and Contributors. 00014 // 00015 // Permission is hereby granted, free of charge, to any person or organization 00016 // obtaining a copy of the software and accompanying documentation covered by 00017 // this license (the "Software") to use, reproduce, display, distribute, 00018 // execute, and transmit the Software, and to prepare derivative works of the 00019 // Software, and to permit third-parties to whom the Software is furnished to 00020 // do so, all subject to the following: 00021 // 00022 // The copyright notices in the Software and this entire statement, including 00023 // the above license grant, this restriction and the following disclaimer, 00024 // must be included in all copies of the Software, in whole or in part, and 00025 // all derivative works of the Software, unless such copies or derivative 00026 // works are solely in the form of machine-executable object code generated by 00027 // a source language processor. 00028 // 00029 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00030 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00031 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00032 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00033 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00034 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00035 // DEALINGS IN THE SOFTWARE. 00036 // 00037 00038 00039 #ifndef Foundation_Timestamp_INCLUDED 00040 #define Foundation_Timestamp_INCLUDED 00041 00042 00043 #include "Poco/Foundation.h" 00044 #include <ctime> 00045 00046 00047 namespace Poco { 00048 00049 00050 class Foundation_API Timestamp 00058 { 00059 public: 00060 typedef Int64 TimeVal; 00061 typedef Int64 UtcTimeVal; 00062 typedef Int64 TimeDiff; 00063 00064 Timestamp(); 00066 00067 Timestamp(TimeVal tv); 00069 00070 Timestamp(const Timestamp& other); 00072 00073 ~Timestamp(); 00075 00076 Timestamp& operator = (const Timestamp& other); 00077 Timestamp& operator = (TimeVal tv); 00078 00079 void swap(Timestamp& timestamp); 00081 00082 void update(); 00084 00085 bool operator == (const Timestamp& ts) const; 00086 bool operator != (const Timestamp& ts) const; 00087 bool operator > (const Timestamp& ts) const; 00088 bool operator >= (const Timestamp& ts) const; 00089 bool operator < (const Timestamp& ts) const; 00090 bool operator <= (const Timestamp& ts) const; 00091 00092 Timestamp operator + (TimeDiff d) const; 00093 Timestamp operator - (TimeDiff d) const; 00094 TimeDiff operator - (const Timestamp& ts) const; 00095 Timestamp& operator += (TimeDiff d); 00096 Timestamp& operator -= (TimeDiff d); 00097 00098 std::time_t epochTime() const; 00102 00103 UtcTimeVal utcTime() const; 00107 00108 TimeVal epochMicroseconds() const; 00111 00112 TimeDiff elapsed() const; 00115 00116 bool isElapsed(TimeDiff interval) const; 00119 00120 static Timestamp fromEpochTime(std::time_t t); 00122 00123 static Timestamp fromUtcTime(UtcTimeVal val); 00125 00126 static TimeVal resolution(); 00130 00131 #if defined(_WIN32) 00132 static Timestamp fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh); 00133 void toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const; 00134 #endif 00135 00136 private: 00137 TimeVal _ts; 00138 }; 00139 00140 00141 // 00142 // inlines 00143 // 00144 inline bool Timestamp::operator == (const Timestamp& ts) const 00145 { 00146 return _ts == ts._ts; 00147 } 00148 00149 00150 inline bool Timestamp::operator != (const Timestamp& ts) const 00151 { 00152 return _ts != ts._ts; 00153 } 00154 00155 00156 inline bool Timestamp::operator > (const Timestamp& ts) const 00157 { 00158 return _ts > ts._ts; 00159 } 00160 00161 00162 inline bool Timestamp::operator >= (const Timestamp& ts) const 00163 { 00164 return _ts >= ts._ts; 00165 } 00166 00167 00168 inline bool Timestamp::operator < (const Timestamp& ts) const 00169 { 00170 return _ts < ts._ts; 00171 } 00172 00173 00174 inline bool Timestamp::operator <= (const Timestamp& ts) const 00175 { 00176 return _ts <= ts._ts; 00177 } 00178 00179 00180 inline Timestamp Timestamp::operator + (Timestamp::TimeDiff d) const 00181 { 00182 return Timestamp(_ts + d); 00183 } 00184 00185 00186 inline Timestamp Timestamp::operator - (Timestamp::TimeDiff d) const 00187 { 00188 return Timestamp(_ts - d); 00189 } 00190 00191 00192 inline Timestamp::TimeDiff Timestamp::operator - (const Timestamp& ts) const 00193 { 00194 return _ts - ts._ts; 00195 } 00196 00197 00198 inline Timestamp& Timestamp::operator += (Timestamp::TimeDiff d) 00199 { 00200 _ts += d; 00201 return *this; 00202 } 00203 00204 00205 inline Timestamp& Timestamp::operator -= (Timestamp::TimeDiff d) 00206 { 00207 _ts -= d; 00208 return *this; 00209 } 00210 00211 00212 inline std::time_t Timestamp::epochTime() const 00213 { 00214 return std::time_t(_ts/resolution()); 00215 } 00216 00217 00218 inline Timestamp::UtcTimeVal Timestamp::utcTime() const 00219 { 00220 return _ts*10 + (TimeDiff(0x01b21dd2) << 32) + 0x13814000; 00221 } 00222 00223 00224 inline Timestamp::TimeVal Timestamp::epochMicroseconds() const 00225 { 00226 return _ts; 00227 } 00228 00229 00230 inline Timestamp::TimeDiff Timestamp::elapsed() const 00231 { 00232 Timestamp now; 00233 return now - *this; 00234 } 00235 00236 00237 inline bool Timestamp::isElapsed(Timestamp::TimeDiff interval) const 00238 { 00239 Timestamp now; 00240 Timestamp::TimeDiff diff = now - *this; 00241 return diff >= interval; 00242 } 00243 00244 00245 inline Timestamp::TimeVal Timestamp::resolution() 00246 { 00247 return 1000000; 00248 } 00249 00250 00251 inline void swap(Timestamp& s1, Timestamp& s2) 00252 { 00253 s1.swap(s2); 00254 } 00255 00256 00257 } // namespace Poco 00258 00259 00260 #endif // Foundation_Timestamp_INCLUDED