00001 /* ES40 emulator. 00002 * Copyright (C) 2007-2008 by the ES40 Emulator Project 00003 * 00004 * WWW : http://sourceforge.net/projects/es40 00005 * E-mail : camiel@camicom.com 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * Although this is not required, the author would appreciate being notified of, 00022 * and receiving any modifications you may make to the source code that might serve 00023 * the general public. 00024 */ 00025 00037 // 00038 // Timestamp.h 00039 // 00040 // $Id: Timestamp.h,v 1.1 2008/03/31 19:13:35 iamcamiel Exp $ 00041 // 00042 // Library: Foundation 00043 // Package: DateTime 00044 // Module: Timestamp 00045 // 00046 // Definition of the Timestamp class. 00047 // 00048 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 00049 // and Contributors. 00050 // 00051 // Permission is hereby granted, free of charge, to any person or organization 00052 // obtaining a copy of the software and accompanying documentation covered by 00053 // this license (the "Software") to use, reproduce, display, distribute, 00054 // execute, and transmit the Software, and to prepare derivative works of the 00055 // Software, and to permit third-parties to whom the Software is furnished to 00056 // do so, all subject to the following: 00057 // 00058 // The copyright notices in the Software and this entire statement, including 00059 // the above license grant, this restriction and the following disclaimer, 00060 // must be included in all copies of the Software, in whole or in part, and 00061 // all derivative works of the Software, unless such copies or derivative 00062 // works are solely in the form of machine-executable object code generated by 00063 // a source language processor. 00064 // 00065 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00066 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00067 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00068 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00069 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00070 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00071 // DEALINGS IN THE SOFTWARE. 00072 // 00073 00074 00075 #ifndef Foundation_Timestamp_INCLUDED 00076 #define Foundation_Timestamp_INCLUDED 00077 00078 00079 #include "VMS/Foundation.h" 00080 #include <ctime> 00081 00082 00083 namespace Poco { 00084 00085 00086 class Foundation_API Timestamp 00094 { 00095 public: 00096 typedef Int64 TimeVal; 00097 typedef Int64 UtcTimeVal; 00098 typedef Int64 TimeDiff; 00099 00100 Timestamp(); 00102 00103 Timestamp(TimeVal tv); 00105 00106 Timestamp(const Timestamp& other); 00108 00109 ~Timestamp(); 00111 00112 Timestamp& operator = (const Timestamp& other); 00113 Timestamp& operator = (TimeVal tv); 00114 00115 void swap(Timestamp& timestamp); 00117 00118 void update(); 00120 00121 bool operator == (const Timestamp& ts) const; 00122 bool operator != (const Timestamp& ts) const; 00123 bool operator > (const Timestamp& ts) const; 00124 bool operator >= (const Timestamp& ts) const; 00125 bool operator < (const Timestamp& ts) const; 00126 bool operator <= (const Timestamp& ts) const; 00127 00128 Timestamp operator + (TimeDiff d) const; 00129 Timestamp operator - (TimeDiff d) const; 00130 TimeDiff operator - (const Timestamp& ts) const; 00131 Timestamp& operator += (TimeDiff d); 00132 Timestamp& operator -= (TimeDiff d); 00133 00134 std::time_t epochTime() const; 00138 00139 UtcTimeVal utcTime() const; 00143 00144 TimeVal epochMicroseconds() const; 00147 00148 TimeDiff elapsed() const; 00151 00152 bool isElapsed(TimeDiff interval) const; 00155 00156 static Timestamp fromEpochTime(std::time_t t); 00158 00159 static Timestamp fromUtcTime(UtcTimeVal val); 00161 00162 static TimeVal resolution(); 00166 00167 #if defined(_WIN32) 00168 static Timestamp fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh); 00169 void toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const; 00170 #endif 00171 00172 private: 00173 TimeVal _ts; 00174 }; 00175 00176 00177 // 00178 // inlines 00179 // 00180 inline bool Timestamp::operator == (const Timestamp& ts) const 00181 { 00182 return _ts == ts._ts; 00183 } 00184 00185 00186 inline bool Timestamp::operator != (const Timestamp& ts) const 00187 { 00188 return _ts != ts._ts; 00189 } 00190 00191 00192 inline bool Timestamp::operator > (const Timestamp& ts) const 00193 { 00194 return _ts > ts._ts; 00195 } 00196 00197 00198 inline bool Timestamp::operator >= (const Timestamp& ts) const 00199 { 00200 return _ts >= ts._ts; 00201 } 00202 00203 00204 inline bool Timestamp::operator < (const Timestamp& ts) const 00205 { 00206 return _ts < ts._ts; 00207 } 00208 00209 00210 inline bool Timestamp::operator <= (const Timestamp& ts) const 00211 { 00212 return _ts <= ts._ts; 00213 } 00214 00215 00216 inline Timestamp Timestamp::operator + (Timestamp::TimeDiff d) const 00217 { 00218 return Timestamp(_ts + d); 00219 } 00220 00221 00222 inline Timestamp Timestamp::operator - (Timestamp::TimeDiff d) const 00223 { 00224 return Timestamp(_ts - d); 00225 } 00226 00227 00228 inline Timestamp::TimeDiff Timestamp::operator - (const Timestamp& ts) const 00229 { 00230 return _ts - ts._ts; 00231 } 00232 00233 00234 inline Timestamp& Timestamp::operator += (Timestamp::TimeDiff d) 00235 { 00236 _ts += d; 00237 return *this; 00238 } 00239 00240 00241 inline Timestamp& Timestamp::operator -= (Timestamp::TimeDiff d) 00242 { 00243 _ts -= d; 00244 return *this; 00245 } 00246 00247 00248 inline std::time_t Timestamp::epochTime() const 00249 { 00250 return std::time_t(_ts/resolution()); 00251 } 00252 00253 00254 inline Timestamp::UtcTimeVal Timestamp::utcTime() const 00255 { 00256 return _ts*10 + (TimeDiff(0x01b21dd2) << 32) + 0x13814000; 00257 } 00258 00259 00260 inline Timestamp::TimeVal Timestamp::epochMicroseconds() const 00261 { 00262 return _ts; 00263 } 00264 00265 00266 inline Timestamp::TimeDiff Timestamp::elapsed() const 00267 { 00268 Timestamp now; 00269 return now - *this; 00270 } 00271 00272 00273 inline bool Timestamp::isElapsed(Timestamp::TimeDiff interval) const 00274 { 00275 Timestamp now; 00276 Timestamp::TimeDiff diff = now - *this; 00277 return diff >= interval; 00278 } 00279 00280 00281 inline Timestamp::TimeVal Timestamp::resolution() 00282 { 00283 return 1000000; 00284 } 00285 00286 00287 inline void swap(Timestamp& s1, Timestamp& s2) 00288 { 00289 s1.swap(s2); 00290 } 00291 00292 00293 } // namespace Poco 00294 00295 00296 #endif // Foundation_Timestamp_INCLUDED