标准C++写的DateTime对象

头文件

  1#pragma once
  2//#include <eh.h>
  3#include <windows.h>
  4#include <string>
  5#include <stdio.h>
  6#include <time.h>
  7using namespace std;
  8
  9namespace HongYing
 10{
 11
 12    namespace IO
 13    {
 14
 15        class FileStream
 16        {
 17        public:
 18            FileStream(void);
 19            FileStream(const char * filePath);
 20            void Open(const char * filePath,int mode);
 21            void Close(void);
 22            char Putc(char c); //写入一个字符到流中。
 23            char Getc();//从流中读入一个字符。
 24            void Seek(long offset,int seek_origin);//定位到流中指定的位置以字节为单位,成功返回 0
 25            char * Gets(char * buffer,int maxCount) ; //从流中读一行或指定个字符 失败返回NULL
 26            void Puts(char * buffer) ;//写一个行字符串到流中;
 27            int End();
 28            int Error();
 29            void Rewind();//其实本函数相当于fseek(fp,0L,SEEK_SET);
 30            int Read(void *buffer,size_t size,size_t count);//从流中读指定块数的字符,反回实际读入的块数 size 为块的大小,count 要读的块数
 31            int Write(void *buffer,size_t size,size_t count);//向流中写指定的数据
 32            FILE * GetFilePtr(void);
 33        public:
 34            ~FileStream(void);
 35        private:
 36            // 文件指针
 37            FILE *file;
 38        }
;
 39        class File
 40        {
 41        public:
 42            File(void);
 43            static FileStream Open(const char * filePath,int mode);
 44            static FileStream Create(const char * filePath);
 45            static int Delete(const char *filePath);
 46            static FileStream CreateTempFile();
 47        public:
 48            ~File(void);
 49        }
;
 50
 51        class OpenFileMode
 52        {
 53        public:
 54            OpenFileMode(void);
 55        public:
 56            ~OpenFileMode(void);
 57        public:
 58            // 打开一个用于读取的文本文件
 59            static const int TxtOpenRead = 0;
 60            //创建一个用于写入的文本文件
 61            static const int TxtCreateWrite = 1;
 62            //附加到一个文本文件
 63            static const int TxtAppend = 2;
 64            //打开一个用于读/写的文本文件
 65            static const int TxtOpenReadOrWrite = 3;
 66            //创建一个用于读/写的文本文件
 67            static const int TxtCreateReadOrWrite = 4;
 68            //打开一个用于读/写的文本文件
 69            static const int TxtOpenAppendReadOrWrite=5;
 70            //打开一个用于读取的二进制文件
 71            static const int BitOpenRead = 6;
 72            //创建一个用于写入的二进制文件
 73            static const int BitCreateWrite = 7;
 74            //附加到一个二进制文件
 75            static const int BitAppend = 8;
 76            //打开一个用于读/写的二进制文件
 77            static const int BitOpenReadOrWrite = 9;
 78            //创建一个用于读/写的二进制文件
 79            static const int BitCreateReadOrWrite = 10;
 80            //打开一个用于读/写的二进制文件
 81            static const int BitOpenAppendReadOrWrite =11;
 82            //得到打开文件mode的字符串
 83            static const char * GetModeStr(int mode);
 84
 85        }
;
 86
 87        class Exception
 88        {
 89        public:
 90            Exception();
 91            Exception(char *msg,DWORD errorNo, char *file,char *function,int l);
 92            DWORD ErrorNo;//错误号
 93            char Message[MAX_PATH];//错误信息
 94            char FilePath[MAX_PATH];//错误文件路径
 95            char Fun[MAX_PATH];//错误函数名
 96            EXCEPTION_POINTERS PException_poists;
 97            INT line;
 98        public:
 99            static void Trans_func( unsigned int u, EXCEPTION_POINTERS *pExp );
100            static void Init();//初始化异常
101            string ToString();
102        public:
103            ~Exception();
104
105
106        }
;
107        #define THROW_EX(Msg,errorNo) throw  Exception(Msg,errorNo,__FILE__,__FUNCTION__,__LINE__);
108    }

109    namespace Object
110    {
111    class Object
112    {
113    public:
114        Object(void);
115        virtual bool Equals(const Object *object= 0;
116        virtual int CompareTo(const Object *value) = 0;
117        virtual std::string ToString() = 0 ;
118
119    public:
120        ~Object(void);
121    }
;
122
123    class DateTime :public Object
124    {
125    public:    
126        DateTime(time_t seconds);
127        DateTime(int year,int month,int day);
128        DateTime(int year,int month,int day,int hour,int minute,int second);
129        DateTime(std::string datetimeStr);//日期字符串格式 年/月/日 时:分:秒 例:2008/02/03 9:30:20 出错返回 01/01/1970 00:00:00
130        DateTime(std::string datetimeStr,std::string formaterStr);
131    private:
132        time_t seconds;//自1970起的秒数
133        tm date;
134    public:
135        void AddYears(const time_t years); //将指定的年份数加到此实例的值上。  
136        void AddMonths(const time_t Months);//将指定的月份数加到此实例的值上。  
137        void AddDays(const time_t days); //将指定的天数加到此实例的值上。 
138        void AddHours(const time_t hours);//将指定的小时数加到此实例的值上。 
139        void AddMinutes(const time_t minutes);//将指定的分钟数加到此实例的值上。  
140        void AddSeconds(const time_t seconds); //将指定的秒数加到此实例的值上。
141        void AddWeeks(const time_t weeks);//将指定的周数加到些实上的值上。
142        static int Compare(const DateTime *value1,const DateTime *value2);//对两个 DateTime 的实例进行比较,并返回一个指示第一个实例是早于、等于还是晚于第二个实例的整数。  返回值:小于零 value1 小于 value2。 零 value1 等于 value2。 大于零 value1 大于 value2。 
143        int CompareTo(const Object *value);//已重载。 将此实例的值与指定的 DateTime 值相比较,并指示此实例是早于、等于还是晚于指定的 DateTime 值。  
144        int CompareTo(const DateTime *value);//小于零 此实例小于 value。 零 此实例等于 value。 大于零 此实例大于 value。 
145
146
147        int DaysInMonth(const int year,const int months);//返回指定年和月中的天数。    
148        bool Equals(const Object *object);
149        bool Equals(const DateTime *dateTime);
150        static bool Equals(const DateTime *value1,const DateTime *value2);
151        static DateTime Parse(std::string datetimeStr);//日期字符串格式 月/日/年 时:分:秒 例:02/03/2008 9:30:20 出错返回 01/01/1970 00:00:00
152        static DateTime Parse(std::string dateTimeStr,std::string formaterStr);
153        std::string ToShortDateString();//将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。  
154        std::string ToString();
155        std::string ToString(const std::string formaterStr);//formaterStr = "%Y-%m-%d %H:%M:%S" %Y=年 %m=月 %d=日 %H=时 %M=分 %S=秒
156    public:        
157        int GetYear();//获取此实例所表示日期的年份部分。
158        int GetMonth();//获取此实例所表示日期的年份部分。
159        int GetDay();// 获取此实例所表示的日期为该月中的第几天。
160        int GetHour();//获取此实例所表示日期的小时部分。 
161        int GetMinute();//获取此实例所表示日期的分钟部分
162        int GetSecond();//获取此实例所表示日期的秒部分。 
163        int DayOfWeek(); //获取此实例所表示的日期是星期几。
164        int DayOfYear();//记录今天是一年里面的第几天,从1月1日起,0-365
165        static DateTime GetNow(); //返回当前日期是间
166    public:
167        bool operator == (DateTime &dateTime);
168        bool operator > (DateTime &dateTime);
169        bool operator < (DateTime &DateTime);
170        bool operator >= (DateTime &DateTime);
171        bool operator <= (DateTime &DateTime);
172        bool operator != (DateTime &DateTime);
173
174    private:
175        void InitByStr(std::string dateTimeStr,std::string formaterStr);
176    public:
177        ~DateTime(void);
178    }
;
179
180
181
182
183    }

184}

185

CPP文件

  1#include "StdAfx.h"
  2#include "HongYing.h"
  3namespace HongYing
  4{
  5    namespace Object
  6    {
  7        DateTime::DateTime(const time_t seconds)
  8        {
  9            this->seconds = seconds;
 10            this->date = *localtime(&this->seconds);
 11        }

 12        DateTime::DateTime(int year,int month,int day)
 13        {
 14            tm t;
 15            t.tm_year = year-1900;
 16            t.tm_mon = month-1;
 17            t.tm_mday = day;
 18            t.tm_hour = 0;
 19            t.tm_min = 0;
 20            t.tm_sec = 0;
 21            this->seconds = mktime(&t);
 22            this->date = *localtime(&this->seconds);
 23
 24        }

 25        DateTime::DateTime(int year,int month,int day,int hour,int minute,int second)
 26        {
 27            tm t;
 28            t.tm_year = year-1900;
 29            t.tm_mon = month-1;
 30            t.tm_mday = day;
 31            t.tm_hour = hour;
 32            t.tm_min = minute;
 33            t.tm_sec = second;
 34            this->seconds = mktime(&t);
 35            this->date = *localtime(&this->seconds);
 36
 37        }

 38
 39        DateTime::DateTime(std::string datetimeStr) //日期字符串格式 月/日/年 时:分:秒 例:02/03/2008 9:30:20 出错返回 01/01/1970 00:00:00
 40        {
 41            this->InitByStr(datetimeStr,"%d/%d/%d %d:%d:%d");
 42        }

 43        DateTime::DateTime(std::string datetimeStr,std::string formaterStr)
 44        {
 45            this->InitByStr(datetimeStr,formaterStr);
 46        }

 47        DateTime DateTime::Parse(std::string datetimeStr)
 48        {
 49
 50            DateTime datetime(datetimeStr);
 51            return datetime;
 52        }

 53        DateTime DateTime::Parse(const std::string datetimeStr,const std::string formaterStr)
 54        {
 55            DateTime datetime(datetimeStr,formaterStr);
 56            return datetime;
 57        }

 58
 59
 60        void DateTime::InitByStr(std::string dateTimeStr,std::string formaterStr)
 61        {
 62            int year,month,day,hour,minutes,seconds;
 63            sscanf(dateTimeStr.c_str(),formaterStr.c_str(),&year,&month,&day,&hour,&minutes,&seconds);    
 64            if(year <1900) year = 1970;
 65            if(month <0) month = 1;
 66            if(day <0) day = 1;
 67            if(hour <0) hour = 0;
 68            if(minutes <0) minutes = 0;
 69            if(seconds <0) seconds = 0;
 70            tm t;
 71            t.tm_year = year-1900;
 72            t.tm_mon = month-1;
 73            t.tm_mday = day;
 74            t.tm_hour = hour;
 75            t.tm_min = minutes;
 76            t.tm_sec = seconds;
 77            this->seconds = mktime(&t);
 78            this->date = *localtime(&this->seconds);
 79        }

 80        int DateTime::DayOfWeek()
 81        {
 82            return this->date.tm_wday;
 83        }

 84        int DateTime::DayOfYear()
 85        {
 86            return this->date.tm_yday;
 87        }

 88        int DateTime::DaysInMonth(const int year,const int months)
 89        {
 90            return 0;
 91        }

 92
 93        bool DateTime::Equals(const Object *object)
 94        {
 95            DateTime *dateTime = (DateTime *)object;
 96            if(this->seconds == dateTime->seconds)
 97                return true;
 98            return false;
 99        }

100
101        bool DateTime::Equals(const DateTime *dateTime)
102        {
103            return this->Equals((Object *)dateTime);
104        }

105
106        bool DateTime::Equals(const DateTime *value1,const DateTime *value2)
107        {
108            if(value1->seconds == value2->seconds)
109                return true;
110            return false;
111        }

112        int DateTime::GetDay()
113        {    
114            return this->date.tm_mday;;
115        }

116
117        int DateTime::GetHour()
118        {
119            return this->date.tm_hour;
120        }

121        int DateTime::GetMinute()
122        {
123            return this->date.tm_min;
124        }

125        int DateTime::GetMonth()
126        {
127            return this->date.tm_mon;
128        }

129
130        DateTime DateTime::GetNow()
131        {
132            DateTime datetime(time(0));
133            return datetime;
134        }

135
136        int DateTime::GetSecond()
137        {
138            return this->date.tm_sec;
139        }

140
141        int DateTime::GetYear()
142        {
143            return this->date.tm_year+1900;
144        }

145
146        void DateTime::AddYears(const time_t years)
147        {
148            this->date.tm_year = this->date.tm_year+years;
149            this->seconds = mktime(&this->date);
150        }

151        void DateTime::AddMonths(const time_t months)
152        {
153            int a  =(int)((this->date.tm_mon+months)/12);
154            
155            this->date.tm_year = this->date.tm_year+a;
156            this->date.tm_mon = this->date.tm_mon+(int)((this->date.tm_mon+months)%12)-1;
157            this->seconds = mktime(&this->date);
158        }

159
160        void DateTime::AddDays(const time_t days)
161        {    
162            this->AddHours(days*24);
163        }

164        void DateTime::AddHours(const time_t hours)
165        {
166            this->AddMinutes(hours*60);
167        }

168        void DateTime::AddMinutes(const time_t minutes)
169        {
170            this->AddSeconds(minutes *60);
171        }

172
173        void DateTime::AddSeconds(const time_t seconds)
174        {
175            this->seconds = this->seconds+seconds;
176            this->date = *localtime(&this->seconds);
177        }

178        void DateTime::AddWeeks(const time_t weeks)
179        {
180            this->AddDays(weeks*7);
181        }

182
183
184        int DateTime::Compare(const DateTime *t1,const DateTime *t2)
185        {
186            if( t1->seconds == t2->seconds)
187                return 0;
188            if(t1->seconds < t2->seconds)
189                return -1;
190            return 1;
191
192        }

193
194
195        int DateTime::CompareTo(const Object *value)
196        {
197            DateTime * dateTime =  (DateTime *)value;
198            if( dateTime->seconds == this->seconds)
199                return 0;
200            if(this->seconds < dateTime->seconds)
201                return -1;
202            return 1;
203        }

204
205        int DateTime::CompareTo(const DateTime *value)
206        {
207            if(this->seconds == value->seconds)
208                return 0;
209            if(this->seconds <value->seconds)
210                return -1;
211            return 1;
212        }

213
214
215        std::string DateTime::ToShortDateString()
216        {
217            return this->ToString("%Y-%m-%d");
218        }

219
220        std::string DateTime::ToString()
221        {
222            return this->ToString("%Y-%m-%d %H:%M:%S");
223        }

224        std::string DateTime::ToString(const std::string formaterStr)
225        {
226            //strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t)); 
227            char s[256];
228            strftime(s, sizeof(s), formaterStr.c_str(), &this->date);
229            std::string str(s);
230            return str;
231
232        }

233
234        bool DateTime::operator ==( DateTime &dateTime)
235        {
236            return this->Equals(&dateTime);
237
238        }

239        bool DateTime::operator != (DateTime &datetime)
240        {
241            if(this->seconds != datetime.seconds)
242                return true;
243            return false;
244        }

245        bool DateTime::operator > (DateTime &dateTime)
246        {
247            if(this->seconds > dateTime.seconds)
248                return true;
249            return false;
250        }

251        bool DateTime::operator < (DateTime &datetime)
252        {
253            if(this->seconds <datetime.seconds)
254                return true;
255            return false;
256        }

257        bool DateTime::operator >=(DateTime &datetime)
258        {
259            if(this->seconds >= datetime.seconds)
260                return true;
261            return false;
262        }

263
264        bool DateTime::operator <=(DateTime &datetime)
265        {
266            if(this->seconds <= datetime.seconds)
267                return true;
268            return false;
269        }

270
271        DateTime::~DateTime(void)
272        {
273
274        }

275
276    }

277
278}

279
原文地址:https://www.cnblogs.com/wubiyu/p/1345369.html