/********************************************************************* * * 文 件: TCMTime.h 通用的时间处理接口 * * 版权所有: Shanghai Baosight Software Co., Ltd. * * 概述:提供通用的时间处理操作,包括获取当前时间,进行时间的转换等 * : * : * * 版本历史 * 1.0 2010-06-17 echo_li 由连退项目导入 * %USER% *********************************************************************/ #ifndef TCMTIME_H_ #define TCMTIME_H_ #include #include #include #ifdef _WIN32 # include # include # include #pragma warning (disable : 4996) #else # include #endif #ifdef _WIN32 #define timezone _timezone #else #define timezone __timezone #endif using namespace std; const time_t TIMEMAX = 2147426047; namespace baosight{ enum MonthsEnum { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; /********************************************************************* * 类 名: TCMTime * 版权所有: Shanghai Baosight Software Co., Ltd. * 类 职 责:提供通用的时间处理操作,包括获取当前时间,进行时间的转换等 * : * : * 版本历史 * 1.0 2010-06-17 echo_li 由连退项目导入 * *********************************************************************/ class TCMTime { public: TCMTime(); TCMTime(time_t now);//使用秒数进行初始化 TCMTime(struct tm now);//使用struct tm进行初始化 TCMTime(const TCMTime& now);//拷贝构造 TCMTime(const string &str,string pattern="yyyyMMddhhmiss"); public: ~TCMTime(); public: TCMTime& operator +(const long& second);//加上一个秒数 TCMTime& operator -(const long& second);//减去一个秒数 TCMTime& operator =(const TCMTime &curTime); bool operator ==(const TCMTime &ComTime); bool operator !=(const TCMTime &ComTime); TCMTime& operator +=(const long& sec);//加上一个秒数 TCMTime& operator -=(const long& sec);//减去一个秒数 friend long operator -(TCMTime& t1,TCMTime&t2);//返回2个时间相差的妙数 public: /********************************************************************** * 概述: 设置系统时间 * 函数名: SetSystemTime * 返回值: short * 参数列表: 参数类型 取值范围 描述 * * 版本历史 * 1.0 2010-09-02 echo_li 增加注释 * **********************************************************************/ short SetSystemTime();//设置系统时间 public: /********************************************************************** * 概述: 返回当前时间距离1970年的秒数 * 函数名: GetSec70 * 返回值: time_t * 参数列表: 参数类型 取值范围 描述 * * 版本历史 * 1.0 2010-09-02 echo_li 增加注释 * **********************************************************************/ time_t GetSec70()const;// /********************************************************************** * 概述: 返回时分秒距离零点的偏移 * 函数名: GetTimeOffset * 返回值: int * 参数列表: 参数类型 取值范围 描述 * * 版本历史 * 1.0 2010-09-02 echo_li 增加注释 * **********************************************************************/ int GetTimeOffset();// public: int Year(){return m_year;}//取年 int Month(){return m_month;}//取月 int Day(){return m_day;}//取日 int Hour(){return m_hour;}//取时 int Minute(){return m_minute;}//取分 int Second(){return m_second;}//取秒 public: string ToString(char* format=NULL);//转化成String time_t ToSec70();//转换成日历时间 public: /********************************************************************** * 概述: 取当前时区当前时间 * 函数名: Now * 返回值: baosight::TCMTime * 参数列表: 参数类型 取值范围 描述 * * 版本历史 * 1.0 2010-09-02 echo_li 增加注释 * **********************************************************************/ static TCMTime Now();// /********************************************************************** * 概述: 取gtm当前时间 * 函数名: GTMNow * 返回值: baosight::TCMTime * 参数列表: 参数类型 取值范围 描述 * * 版本历史 * 1.0 2010-09-02 echo_li 增加注释 * **********************************************************************/ static TCMTime GTMNow();// static string ToString(time_t t,char* format=NULL);//将time_t形式的时间转换成string形式 static time_t ToSec70(string t);//将string形式的时间转换成time_t public: /********************************************************************** * 概述: 取得日期,将时分秒置0 * 函数名: SetTimeZone * 返回值: void * 参数列表: 参数类型 取值范围 描述 * * 版本历史 * 1.0 2010-09-02 echo_li 增加注释 * **********************************************************************/ void SetTimeZone();// void AddOneDay();//将时间加一天 void SubOneDay();//将时间减一天 private: void SetTime(struct tm TM); private: int m_year; MonthsEnum m_month; int m_day; int m_hour; int m_minute; int m_second; time_t m_sec70; private: const static int MAX_YEAR = 2038; const static int MAX_MONTH = 1; const static int MAX_DAY = 18; const static int MAX_HOUR = 19; const static int MAX_MINUTE = 14; const static int MAX_SECOND = 7; const static int MIN_YEAR = 1970; const static int MIN_MONTH = 1; const static int MIN_DAY = 1; const static int MIN_HOUR = 0; const static int MIN_MINUTE = 0; const static int MIN_SECOND = 0; const static int DAY_SECONDS = 86400; }; long operator -(TCMTime& t1,TCMTime&t2); //声明重载操作符 } #endif