00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 00022 00023 // JTime.h: interface for the JTime class. 00024 // 00025 ////////////////////////////////////////////////////////////////////// 00026 00027 #if !defined(AFX_TIME_H__33DD983B_D556_470C_80BD_2BE37411D47A__INCLUDED_) 00028 #define AFX_TIME_H__33DD983B_D556_470C_80BD_2BE37411D47A__INCLUDED_ 00029 00030 //#if _MSC_VER > 1000 00031 //#pragma once 00032 //#endif // _MSC_VER > 1000 00033 00034 #include "standard.h" 00035 #include <stdio.h> 00036 #include <string.h> 00037 #include <time.h> 00038 #if defined(WIN32_POCKETPC) 00039 00040 #include "wce_time.h" 00041 00042 #else 00043 #include <sys/timeb.h> 00044 #endif 00045 #include "Object.h" 00046 #include "JString.h" 00047 00048 #ifndef WIN32 00049 #include <sys/time.h> 00050 #endif 00051 00052 namespace cmlabs { 00053 00054 class JTime : public Object 00055 { 00056 public: 00057 #ifdef WIN32 00058 JTime(FILETIME filetime); 00059 static FILETIME timeb2filetime(struct timeb tb); 00060 static FILETIME time_t2filetime(time_t t); 00061 static long filetime2time_t(FILETIME * ptr); 00062 static struct timeb filetime2timeb(FILETIME * ptr); 00063 #endif 00064 00065 static JTime createInvalid(); 00066 JTime(); 00067 JTime(const JTime &t); 00068 JTime(struct timeb tb); 00069 JTime(const JString& text); 00070 JTime(XMLNode* node); 00071 JTime(long ms); 00072 JTime(long sec, int milli); 00073 ~JTime(); 00074 // virtual ~JTime(); 00075 Object* clone() const; 00076 unsigned long getHash() const; 00077 bool reset(); 00078 bool perfCountReset(); 00079 bool equals(const Object* o2) const; 00080 //! Get total size of payload 00081 virtual unsigned long getPayloadSize() const; 00082 00083 bool unitTest(); 00084 JString toXML() ; 00085 JString toXML(const JString& tagname, const JString& params = "") ; 00086 bool fromXML(XMLNode* node); 00087 00088 bool setTime(int year, int month, int day, int hour, int min, int sec, int ms = 0); 00089 bool setLocalTimeZoneInformation(); 00090 JString getLocalTimeZoneInformation() const; 00091 JString getLocalTimeZoneName() const; 00092 int getLocalTimeZoneOffset() const; 00093 int getLocalTimeZoneDST() const; 00094 JString getOriginalTimeZoneName() const; 00095 int getOriginalTimeZoneOffset() const; 00096 int getOriginalTimeZoneDST() const; 00097 int getTimeDifference() const; 00098 int getTimeDifference(const JTime &t) const; 00099 00100 bool operator>(const JTime &t) const; 00101 bool operator>=(const JTime &t) const; 00102 bool operator<(const JTime &t) const; 00103 bool operator<=(const JTime &t) const; 00104 bool operator==(const JTime &t) const; 00105 bool operator!=(const JTime &t) const; 00106 // const JTime& operator-=(long millis); 00107 // const JTime& operator+=(long millis); 00108 const JTime& operator-=(long long millis); 00109 const JTime& operator+=(long long millis); 00110 int compare(const Object* o2) const; 00111 00112 long operator-(const JTime &t) const; 00113 // JTime operator+(long millis) const; 00114 // JTime operator-(long millis) const; 00115 JTime operator+(long long millis) const; 00116 JTime operator-(long long millis) const; 00117 00118 // bool addMilliseconds(long millis); 00119 bool addMilliseconds(long long millis); 00120 long microDifference(const JTime& t) const; 00121 00122 long getAge() const; 00123 long long getMicroAge() const; 00124 bool isOlderThan(long ms) const; 00125 00126 //! Nice long date and time 00127 JString print() ; 00128 //! Only time part (hour, minute, second, ms) 00129 JString printTime() const; 00130 //! Only time part (hour, minute, second) 00131 JString printTimeSec() const; 00132 //! Only time part (hour, minute, second, ms) 00133 JString printTimeMS() const; 00134 //! Very short date print 00135 JString printDateShort() const; 00136 //! Sortable yyyymmdd 00137 JString printDateSortable() const; 00138 //! Nice long date 00139 JString printDateLong() const; 00140 //! Sortable date and time 00141 JString printStamp() const; 00142 //! HH:MM:SS.ms and days, months, years as needed 00143 JString printDifTime() const; 00144 00145 JString toHTTP() const; 00146 bool fromHTTP(const JString& text); 00147 00148 //! Return full time in ms 00149 double ms() const; 00150 //! Returns full time in ms as string 00151 JString asText() const; 00152 00153 //! Return sec part of time only 00154 long seconds() const; 00155 //! Return ms part of time only 00156 int milliseconds() const; 00157 00158 void setInvalid(); 00159 bool isValid() const; 00160 bool valid; 00161 00162 struct timeb timebuffer; 00163 JString tzc; 00164 unsigned int atomicOffset; 00165 00166 #ifdef WIN32 00167 LARGE_INTEGER perfCount; 00168 #else 00169 struct timeval perfCount; 00170 #endif 00171 00172 private: 00173 }; 00174 00175 00176 00177 } // namespace cmlabs 00178 00179 #include "Collections.h" 00180 00181 namespace cmlabs { 00182 00183 class StatSample; 00184 00185 class StopWatch : public Object 00186 { 00187 public: 00188 StopWatch(); 00189 virtual ~StopWatch(); 00190 00191 Object* clone() const; 00192 00193 bool reset(); 00194 bool start(); 00195 bool stop(); 00196 bool setMark(); 00197 bool setMark(const JString& label); 00198 JString print(); 00199 JString printNoLabels(); 00200 JString printDetails(); 00201 00202 long getTotalTime(); 00203 long getMarkCount(); 00204 long getAverageDistance(); 00205 long getStandardDeviationDistance(); 00206 StatSample getStatSample(); 00207 00208 ObjectDictionary marks; 00209 JTime timeStarted; 00210 JTime timeStopped; 00211 }; 00212 00213 } // namespace cmlabs 00214 00215 #include "MathClasses.h" 00216 00217 #endif // !defined(AFX_TIME_H__33DD983B_D556_470C_80BD_2BE37411D47A__INCLUDED_)