C++每日练笔之日期类(基类)

 1 在我的命名空间TYCppStdLib中对于日期和时间的操作非常丰富。
 2 共有六个类和一组全局函数。
 3 六个类分别是:
 4 CDate
 5 CTime
 6 CDateAndTime
 7 CMyDate
 8 CMyTime
 9 CMyDateAndTime
10 
11 前三个类不带类的成员变量,直接操作用户给定的数据。
12 前三个类的成员函数与全局函数一一对应。
13 后三个类带类的成员变量,成员函数操作类的成员变量。
14 
15 现在发布的是CDate类(基类,完成基本功能,待更新)。

 

 1 /*- ==========================================================
 2 *     文件名  :CDate.h
 3 *     开发人员:袁培荣
 4 *     当前版本:1.0.0.2595
 5 *     创建时间:2012-04-23
 6 *     修改时间:2012-05-01
 7 *     功能说明:日期类(基类)
 8 *     版权说明:版权所有 袁培荣 YuanPeirong 
 9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
11                 MinGW 2011118
12                 Visual C++ 6.0 SP6(中文企业版)
13 - ==========================================================*/
14 
15 #ifndef CDate_H_TYCppStdLib
16 #define CDate_H_TYCppStdLib
17 
18 #ifdef CDate_DLL_API
19 #else
20 #define CDate_DLL_API _declspec(dllimport)
21 #endif
22 
23 #include <string>
24 #include "Windows.h"
25 
26 using namespace std;
27 
28 namespace TYCppStdLib
29 {   
30     class CDate_DLL_API CDate
31     {
32     public:
33         CDate();
34         virtual ~CDate();
35         
36         virtual int GetYear();   // 获取年
37         virtual int GetMonth();  // 获取月
38         virtual int GetDay();    // 获取日
39         
40         virtual bool SetYear(int year);    // 设置年
41         virtual bool SetMonth(int month);  // 设置月
42         virtual bool SetDay(int day);      // 设置日
43         
44         // 获取日期
45         virtual void GetDate(int &year,int &month,int &day);
46         //virtual string GetDate(const string strDivide="-");
47         //virtual void GetDate(char *dates,char *divide="-");
48         //virtual void GetDate(CMyDate &ob);
49         
50         // 设置日期
51         virtual bool SetDate(int year,int month,int day); 
52         //virtual bool SetDate(char *dates);
53         
54         // 检查日期是否合法
55         virtual bool DateIsValid(int year,int month,int day);
56         // 判断闰年
57         virtual bool IsLeapYear(int year=0);  
58         // 计算某年天数
59         virtual int GetDaysOfYear(int year=0); 
60         //计算某年某月天数
61         virtual int GetDaysOfMonth(int year=0,int month=0);
62         // 获取本年已过天数
63         virtual int GetDaysOver(int year,int month,int day);
64     };
65 }
66 
67 #endif
  1 /*- ==========================================================
  2 *     文件名  :CDate.cpp
  3 *     开发人员:袁培荣
  4 *     当前版本:1.0.0.2595
  5 *     创建时间:2012-04-23
  6 *     修改时间:2012-05-01
  7 *     功能说明:日期类(基类)
  8 *     版权说明:版权所有 袁培荣 YuanPeirong 
  9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
 10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
 11                 MinGW 2011118
 12                 Visual C++ 6.0 SP6(中文企业版)
 13 - ==========================================================*/
 14 
 15 #ifndef CDate_DLL_ForAPI
 16 #define CDate_DLL_ForAPI
 17 
 18 #define CDate_DLL_API _declspec(dllexport)
 19 
 20 #endif
 21 
 22 #include "http://www.cnblogs.com/Include/DateAndTime/CDate.h"
 23 //#include "stdio.h"
 24 // #include <cstdio>
 25 //#include "string.h"
 26 // #include <cstring>
 27 // #include <cstdlib>
 28 //using std::sprintf;
 29 // using std::strcat;
 30 // using std::strcpy;
 31 
 32 /*     typedef struct _SYSTEMTIME { // st 
 33     WORD wYear; 
 34     WORD wMonth; 
 35     WORD wDayOfWeek; 
 36     WORD wDay; 
 37     WORD wHour; 
 38     WORD wMinute; 
 39     WORD wSecond; 
 40     WORD wMilliseconds; 
 41     } SYSTEMTIME; */
 42 
 43 //构造函数
 44 TYCppStdLib::CDate::CDate()
 45 {
 46     //空函数
 47 }
 48 
 49 //析构函数
 50 TYCppStdLib::CDate::~CDate()
 51 {
 52     //空函数
 53 }
 54 
 55 // 获取年
 56 int TYCppStdLib::CDate::GetYear()
 57 {
 58     SYSTEMTIME st;
 59     GetLocalTime(&st);
 60     return st.wYear;
 61 }
 62  
 63 // 获取月  
 64 int TYCppStdLib::CDate::GetMonth()
 65 {
 66     SYSTEMTIME st;
 67     GetLocalTime(&st);
 68     return st.wMonth;
 69 }
 70  
 71 // 获取日 
 72 int TYCppStdLib::CDate::GetDay()
 73 {
 74     SYSTEMTIME st;
 75     GetLocalTime(&st);
 76     return st.wDay;
 77 }
 78 
 79 // 设置年
 80 bool TYCppStdLib::CDate::SetYear(int year)
 81 {
 82     SYSTEMTIME st;
 83     if(year<1970)
 84         return false;
 85     GetLocalTime(&st);
 86     st.wYear=year;
 87     if(SetLocalTime(&st))
 88         return true;
 89     return false;
 90 }
 91 
 92 // 设置月    
 93 bool TYCppStdLib::CDate::SetMonth(int month)
 94 {
 95     SYSTEMTIME st;
 96     if(month<1 || month>12)
 97         return false;
 98     GetLocalTime(&st);
 99     st.wMonth=month;
100     if(SetLocalTime(&st))
101         return true;
102     return false;
103 }
104   
105 // 设置日
106 bool TYCppStdLib::CDate::SetDay(int day)
107 {
108     SYSTEMTIME st;
109     int t;
110     GetLocalTime(&st);
111     t=GetDaysOfMonth();
112     if(day<1 || day>t)
113         return false;
114     st.wDay=day;
115     if(SetLocalTime(&st))
116         return true;
117     return false;
118 }
119   
120 // 获取日期  
121 void TYCppStdLib::CDate::GetDate(int &year,int &month,int &day)
122 {
123     year=GetYear();
124     month=GetMonth();
125     day=GetDay();
126 }
127   
128  // void TYCppStdLib::CDate::GetDate(char *dates,char *divide="-")
129 // {
130     // int year, month, day;
131     // char *cyear, *cmonth ,*cday;
132     // year=GetYear();
133     // month=GetMonth();
134     // day=GetDay();
135     
136      // strcpy(dates,year,4,RIGHTSIDE,'0',divide);
137     // strcat(dates,month,2,RIGHTSIDE,'0',divide);
138     // strcat(dates,day,2,RIGHTSIDE,'0'); 
139     
140      // sprintf(cyear, "%d", year);
141     // sprintf(cmonth, "%d", month);
142     // sprintf(cday, "%d", day);
143 
144     // itoa(year, cyear, 10);
145     // itoa(month, cmonth, 10);
146     // itoa(day, cday, 10);
147     
148     // strcpy(dates, cyear);
149     // strcat(dates, divide);
150     // strcat(dates, month);
151     // strcat(dates, divide);
152     // strcat(dates, day);
153 // }
154 
155 // void TYCppStdLib::CDate::GetDate(CDate &ob)
156 // {
157 
158 // } 
159     
160 // 设置日期  
161 bool TYCppStdLib::CDate::SetDate(int year,int month,int day)
162 {
163     if(!SetYear(year))
164         return false;
165     if(!SetMonth(month))
166         return false;
167     return SetDay(day);
168 }
169  
170 // bool TYCppStdLib::CDate::SetDate(char *dates)
171 // {
172 
173 // }
174 
175 // 检查日期是否合法                 
176 bool TYCppStdLib::CDateAndTime::DateIsValid(int year,int month,int day)
177 {
178     if(year<0 || day<1)
179         return false;
180     if(month<1 || month>12)
181         return false;
182     int MaxDay=GetDaysOfMonth(year, month);
183     if(day>MaxDay)
184         return false;
185     return true;
186 }
187 
188 // 判断闰年
189 bool TYCppStdLib::CDateAndTime::IsLeapYear(int year)
190 {
191     if(year<0)
192         return false;
193     if(!year)
194     {
195         SYSTEMTIME st;
196         GetLocalTime(&st);
197         year=st.wYear;
198     }
199     if((year%4==0 && year%100!=0) || year%400==0)
200         return true;
201     return false;
202 }
203 
204 // 计算某年天数
205 int TYCppStdLib::CDateAndTime::GetDaysOfYear(int year)
206 {
207     SYSTEMTIME st;
208     GetLocalTime(&st);
209     if(!year)
210         year=st.wYear;
211     if(year<0)
212         return 0;//return false;
213     if(IsLeapYear(year))
214         return 366;
215     else
216         return 365;
217 }
218 
219 //计算某年某月天数
220 int TYCppStdLib::CDateAndTime::GetDaysOfMonth(int year,int month)
221 {
222     SYSTEMTIME st;
223     GetLocalTime(&st);
224     int day, leap;
225     if(!month)
226         month=st.wMonth;
227     if(!year)
228         year=st.wYear;
229     if(year<1970)
230         return 0;//return false;
231     if (month<1 || month>12)
232         return 0;//return false;
233     if(IsLeapYear(year))
234         leap=1;
235     else
236         leap=0;
237     day=31;     //默认31天
238     switch (month)
239     {
240         case 4:
241         case 6:
242         case 9:
243         case 11:
244             day=30;
245             break;
246         case 2:
247             day=28+leap;
248             break;
249     }
250     return day;
251 }

 

posted @ 2012-05-02 13:33  天远  阅读(327)  评论(0)    收藏  举报

版权所有 © 2010-2020 YuanPeirong TianYuan All Rights Reserved. Powered By 天远