📄 wartime.h
字号:
#ifndef WAR_TIME_H#define WAR_TIME_H/* SYSTEM INCLUDES */#ifndef WAR_TIME_H_INCLUDED# define WAR_TIME_H_INCLUDED# include <time.h>#endif/* PROJECT INCLUDES */#ifndef WAR_TYPES_H# include "WarTypes.h"#endif#ifndef WAR_EXCEPTION_H# include "WarException.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */class WarTime;#ifdef GetCurrentTime# undef GetCurrentTime#endif /** strcmp() like comparsion for time values @param w1 Time to compare against w2 @param w2 Time to compare against w1 @return -1 if w1 < w2, 0 if w1 == w2 and > 1 if w1 > w2 */int WarCompareFileTime (WarTime& w1, WarTime& w2);#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********/// ANSI date format#define WAR_TIME_DEFAULT_FMT "%Y-%m-%d %H:%M:%S"/****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplus/**Generic, portable time class. The timer works in \Ref{UTC} internally. If WarTime::Optimize(TRUE) is called, the class will use an internal variable to keep track of the current time, in stead of querying the system clock. This is nice for performance in timer-intensive programs. The internal value must be set by calling WarTime::WarGetCurrentTime(TRUE) at short intervals (depending on the resolution requiered by the timers and time functions).*/class WarTime {public: // ENUMS /// enum DaysInWeekDef { SUN, /// MON, /// TUE, /// WED, /// THU, /// FRI, /// SAT /// }; /// enum MonthsInYear { JAN, /// FEB, /// MAR, /// APR, /// MAY, /// JUN, /// JUL, /// AUG, /// SEP, /// OCT, /// NOV, /// DEC /// }; // LIFECYCLE /** * Default constructor. * * The time is set to the current time. */ WarTime(void); /** * Copy constructor. * * @param val The value to copy to this object. */ WarTime(const WarTime& val); /** * Copy constructor. * * @param val The value to copy to this object. */ WarTime (const time_t val); /** * Copy constructor. * * @param val The value to copy to this object. */ WarTime (const timeval& val); /** * Copy constructor. * * @param val The value to copy to this object. */ WarTime (const war_time_t& val); /** * Copy constructor. * * @param val The value to copy to this object. */ WarTime (const struct tm& val); #ifdef WIN32 /** * Copy constructor. * * Only available on Win32 platforms * * @param val The value to copy to this object. */ WarTime (const FILETIME& val); /** * Copy constructor. * * Only available on Win32 platforms * * @param val The value to copy to this object. */ WarTime (const SYSTEMTIME& val);#endif // WIN32 //@} Constructors /** * Destructor. */ ~WarTime(void); // OPERATORS /** * Assignment operator. * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const WarTime& val); /** * Assignment operator. * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const time_t val); /** * Assignment operator. * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const timeval val); /** * Assignment operator. * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const war_time_t val); /** * Assignment operator. * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const struct tm& rVal); bool operator < (const WarTime& from) const { return mTime < from.mTime; } bool operator == (const WarTime& from) const { return mTime == from.mTime; }#ifdef WIN32 /** * Assignment operator. * * This operator is only awailable under Windows * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const FILETIME& rVal); /** * Assignment operator. * * This operator is only awailable under Windows * * @param val THe value to assign to this object. * * @return A reference to this object. */ WarTime& operator = (const SYSTEMTIME& rVal);#endif // WIN32 // OPERATIONS /** * Set the object to the current time. */ void Reset(const war_uint32_t millisecondTimeout = 0); /** Clear the time value */ void Clear(); /* * System inpependent sleep function with higher * precision than 1 second. * The precision is system-dependent, but can be * assumed to be at least 1/10 second! * * The function can not be trusted for realtime * timing-needs! * * @param milliseconds Time to sleep. */ static void Sleep(const war_uint32_t milliseconds); /* * If enabled, the behaviour of * \Ref{WarGetCurrentTime()} will change to * use a stored time-val鴘e rather than capturing * the system time. * * @param milliseconds enable or disable optimizing. * * @see SetCurrentTime(), GetCurrentTime() */ static void Optimize( const bool milliseconds = true); /** * Sets the object to a "safe" date. the safe date * is defined as 1.1.1990 00:00 */ void SetSafeDate(); /** * Scans a FTP MDTM string and sets the time value * according to it. */ void ScanMDTM(war_ccstr_t TimeStr) throw(WarException); /** Convert the current time value to UTC. * This assumes that the current time is * localtime. * * Note that WarTime normally works in UTC * by default, and don't need conversion. This * function is normally used internally by the * object. */ void ToUTC(); /** Convert the current time value to local time. * * Note that WarTime normally works in UTC * by default, and don't have any way to recognize * that the time value is in local time. This * function is normally used internally by the * object. */ void ToLT(); // ACCESS // INQUIRY /** Get a time_t equalent for the objects time */ time_t GetTime() const; /** Get a timeval equalent for the objects time */ timeval GetTimeval() const; /** Get a struct tm equalent for the objects time */ struct tm GetTm() const throw(WarException); #ifdef WIN32 /** Get a FILETIME equalent for the objects time */ FILETIME GetFiletime() const; /** Get a SYSTEMTIME equalent for the objects time */ SYSTEMTIME GetSystemtime() const;#endif /* * * Formats the objects time to a FTP MDTM string * * @param precision The precision needed in * 1/precision seconds. */ std::string FormatMDTM(const size_t precision) const throw(WarException);; /** Format local time */ const std::string FormatLT(war_ccstr_t Fmt = WAR_TIME_DEFAULT_FMT) const throw(WarException); /** Format GMT time */ const std::string FormatGMT(war_ccstr_t Fmt = WAR_TIME_DEFAULT_FMT) const throw(WarException); /** Get the offset in seconds between local time * and UTC. */ static time_t GetUtcOffset(); /* * Check for timeout. * A timeout as occured if the current time has * passed the time stored in the object. * * @param milliseconds If milliseconds is nonzero, the * timeout is defined as the stored time + ms. */ const bool IsTimeOut( const war_time_t milliseconds = 0) const; /* * Get the difference in ms between the stored * and the current time. * * The value returned is negative if the current * time has passed the stored time. * * @param milliseconds If milliseconds is non-zero, * the time difference is Current Time - * (Stored time + milliseconds) */ const war_time_t GetTimeDiff( const war_time_t milliseconds = 0) const; /* * Get the current time in milliseconds since * january 1st 1070. The time is in UTF (GMT * timezone). * * @param do_force If optimizing has been enable, * and do_force is true, the current time is * captured val the system and atored globally * before the globally stored time is returned. * If the otimizing is off, this parameter is * ignored. * * @see Optimize() */ static war_time_t WarGetCurrentTime( const bool doForce = false); /* * Check if optimizing is enabled * * @return true if optimizig is enabled * * @see Optimize(), WarGetCurrentTime() */ static bool IsOptimazed() { return mgOptimized; } /** Check if there is set a time. * * Used for timers */ bool IsEmpty() const { return 0 == mTime; } war_time_t mTime;protected:private: static bool mgOptimized; static war_time_t mgCurrentTime; // Time at last update#ifdef WIN32 static war_time_t gFiletimeOffset;#endif};/* INLINE METHODS *//* EXTERNAL REFERENCES */#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* _WAR_TIME_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -