这里设计了一个类,通过4个字节存储日期,8个字节存储DateTime,并添加了一些跟时间日期相关的函数, 如:星期几查询,特定日期之后多少天是几号, 两个日期之间相隔多少天等等。对于一般的用途,基本上够用了
头文件:
1 /************************************************************************
2 *
3 * Created by kusamba@126.com at 2010-9-2 16:18:43
4 */
5 #ifndef _datetime_41210572_h_
6 #define _datetime_41210572_h_
7
8
9 extern int whatday(unsigned long date);
10 extern int whatday(int year, int month, int day);
11
12 //////////////////////////////////////////////////////////////////////////
13 #pragma pack(push, 1)
14 struct sDate
15 {
16 unsigned short year;
17 unsigned char month;
18 unsigned char day;
19
20 sDate();
21
22 sDate(int y, int m, int d);
23
24 //lpcszDate:2006-02-03
25 void Parse(const char* lpcszDate);
26 bool Format(char* lpDate);
27
28 //lpcszDate:20060203
29 void Parse2(const char* lpcszDate);
30 bool Format2(char* lpDate);
31
32 void GetCurrentDate();
33
34 sDate operator-(int days);
35 sDate operator+(int days);
36
37 friend int DiffDay(sDate& d1, sDate& d2);/*d1 - d2*/
38 };
39
40
41 struct sDateTime
42 {
43 unsigned short year;
44 unsigned char month;
45 unsigned char day;
46 unsigned char hour;
47 unsigned char minute;
48 unsigned char second;
49
50 sDateTime();
51
52 sDateTime(int y, int m, int d, int h, int mi, int s);
53
54 //lpcszDate:"2006-02-03 05:18:23"
55 void Parse(const char* lpcszDatetime);
56 bool Format(char* lpDateTime);
57
58 void GetCurrentDateTime();
59
60 friend int DateTimeDiffDay(sDateTime& d1, sDateTime& d2);/*d1 - d2*/
61 friend int DateTimeDiffHour(sDateTime& d1, sDateTime& d2);/*d1 - d2*/
62 friend int DateTimeDiffMiniute(sDateTime& d1, sDateTime& d2);/*d1 - d2*/
63 };
64 #pragma pack(pop)
65
66 extern int DiffDay(sDate& d1, sDate& d2);/*d1 - d2*/
67 extern int DateTimeDiffDay(sDateTime& d1, sDateTime& d2);/*d1 - d2*/
68 extern int DateTimeDiffHour(sDateTime& d1, sDateTime& d2);/*d1 - d2*/
69 extern int DateTimeDiffMiniute(sDateTime& d1, sDateTime& d2);/*d1 - d2*/
70
71
72 #endif //_datetime_41210572_h_
cpp文件:
1 #include "stdafx.h"
2 #include <wtypes.h>
3 #include <time.h>
4 #include <math.h>
5
6 #include "datetime.h"
7
8 /************************************************************************
9 蔡勒公式:
10 W = [C/4] - 2C + y + [y/4] + [13 * (M+1) / 5] + d - 1
11
12 C是世纪数减一,y是年份后两位,M是月份,d是日数。
13 1月和2月要按上一年的13月和14月来算,这时C和y均按上一年取值。
14 */
15 int whatday(unsigned long date) // date format: 20070411
16 {
17 int year = date / 10000;
18 int m_d = date - 10000 * year;
19 int month= m_d / 100;
20 int day = m_d - 100 * month;
21
22 int C = year / 100;
23 int y = year - C * 100;
24 int M = month;
25 int d = day;
26 if (M == 1)
27 {
28 M = 13;
29 y = y - 1;
30 }
31 else if (M == 2)
32 {
33 M = 14;
34 y = y - 1;
35 }
36
37 int weekday = ((int)(floor(C/4) - 2 * C + floor(y/4) + floor(13*(M+1)/5) + d - 1)) % 7;
38 if (weekday < 0)
39 {
40 weekday += 7;
41 }
42
43 return weekday;
44 }
45
46 int whatday(int year, int month, int day)
47 {
48 int C = year / 100;
49 int y = year - C * 100;
50 int M = month;
51 int d = day;
52 if (M == 1)
53 {
54 M = 13;
55 y = y - 1;
56 }
57 else if (M == 2)
58 {
59 M = 14;
60 y = y - 1;
61 }
62
63 int weekday = ((int)(floor(C/4) - 2 * C + floor(y/4) + floor(13*(M+1)/5) + d - 1)) % 7;
64 if (weekday < 0)
65 {
66 weekday += 7;
67 }
68
69 return weekday;
70 }
71
72 //////////////////////////////////////////////////////////////////////////
73 #define SECOND_PER_DAY (24*60*60)
74 #define SECOND_PER_HOUR (60*60)
75 #define SECOND_PER_MIN (60)
76 //////////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////////
78 sDate::sDate()
79 {
80 SYSTEMTIME st;
81 GetLocalTime(&st);
82
83 year = (unsigned short)st.wYear;
84 month = (unsigned char )st.wMonth;
85 day = (unsigned char )st.wDay;
86 }
87
88 sDate::sDate(int y, int m, int d)
89 {
90 year = y;
91 month= m;
92 day = d;
93 }
94
95 //lpcszDate:2006-02-03
96 void sDate::Parse(const char* lpcszDate)
97 {
98 if (NULL == lpcszDate)return;
99
100 int tYear, tMonth, tDay;
101 sscanf(lpcszDate, "%d-%d-%d", &tYear, &tMonth, &tDay);
102
103 year = tYear;
104 month = tMonth;
105 day = tDay;
106 }
107
108 bool sDate::Format(char* lpDate)
109 {
110 if (NULL == lpDate)
111 return false;
112
113 sprintf(lpDate, "%d-%02d-%02d", year, month, day);
114 return true;
115 }
116
117 //lpcszDate:20060203
118 void sDate::Parse2(const char* lpcszDate)
119 {
120 if (NULL == lpcszDate)return;
121
122 int tYear, tMonth, tDay;
123 sscanf(lpcszDate, "%d%d%d", &tYear, &tMonth, &tDay);
124
125 year = tYear;
126 month = tMonth;
127 day = tDay;
128 }
129
130 bool sDate::Format2(char* lpDate)
131 {
132 if (NULL == lpDate)
133 return false;
134
135 sprintf(lpDate, "%d%02d%02d", year, month, day);
136 return true;
137 }
138
139 void sDate::GetCurrentDate()
140 {
141 SYSTEMTIME st;
142 GetLocalTime(&st);
143
144 year = (unsigned short)st.wYear;
145 month = (unsigned char )st.wMonth;
146 day = (unsigned char )st.wDay;
147 }
148
149 sDate sDate::operator-(int days)
150 {
151 struct tm t1;
152 memset(&t1, 0, sizeof(tm));
153
154 t1.tm_year = year - 1900;
155 t1.tm_mon = month - 1;
156 t1.tm_mday = day;
157
158 time_t t_1 = mktime(&t1);
159 t_1 -= SECOND_PER_DAY * days;
160
161 t1 = *localtime(&t_1);
162
163 sDate newDate;
164 newDate.year = t1.tm_year + 1900;
165 newDate.month= t1.tm_mon + 1;
166 newDate.day = t1.tm_mday;
167
168 return newDate;
169 }
170
171 sDate sDate::operator+(int days)
172 {
173 struct tm t1;
174 memset(&t1, 0, sizeof(tm));
175
176 t1.tm_year = year - 1900;
177 t1.tm_mon = month - 1;
178 t1.tm_mday = day;
179
180 time_t t_1 = mktime(&t1);
181 t_1 += SECOND_PER_DAY * days;
182
183 t1 = *localtime(&t_1);
184
185 sDate newDate;
186 newDate.year = t1.tm_year + 1900;
187 newDate.month= t1.tm_mon + 1;
188 newDate.day = t1.tm_mday;
189
190 return newDate;
191 }
192
193 //////////////////////////////////////////////////////////////////////////
194 //////////////////////////////////////////////////////////////////////////
195 sDateTime::sDateTime()
196 {
197 SYSTEMTIME st;
198 GetLocalTime(&st);
199
200 year = (unsigned short)st.wYear;
201 month = (unsigned char )st.wMonth;
202 day = (unsigned char )st.wDay;
203 hour = (unsigned char )st.wHour;
204 minute= (unsigned char )st.wMinute;
205 second= (unsigned char )st.wSecond;
206 }
207
208 sDateTime::sDateTime(int y, int m, int d, int h, int mi, int s)
209 {
210 year = y;
211 month= m;
212 day = d;
213 hour = h;
214 minute=mi;
215 second=s;
216 }
217
218 //lpcszDate:"2006-02-03 05:18:23"
219 void sDateTime::Parse(const char* lpcszDatetime)
220 {
221 if(NULL == lpcszDatetime) return;
222
223 int tYear, tMonth, tDay, tHour, tMin, tSecond;
224 sscanf(lpcszDatetime, "%d-%d-%d %d:%d:%d", &tYear, &tMonth, &tDay, &tHour, &tMin, &tSecond);
225
226 year = tYear;
227 month = tMonth;
228 day = tDay;
229 hour = tHour;
230 minute = tMin;
231 second = tSecond;
232 }
233
234 bool sDateTime::Format(char* lpDateTime)
235 {
236 if (NULL == lpDateTime)
237 return false;
238
239 sprintf(lpDateTime, "%d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
240 return true;
241 }
242
243 void sDateTime::GetCurrentDateTime()
244 {
245 SYSTEMTIME st;
246 GetLocalTime(&st);
247
248 year = (unsigned short)st.wYear;
249 month = (unsigned char )st.wMonth;
250 day = (unsigned char )st.wDay;
251 hour = (unsigned char )st.wHour;
252 minute= (unsigned char )st.wMinute;
253 second= (unsigned char )st.wSecond;
254 }
255
256 //////////////////////////////////////////////////////////////////////////
257 //////////////////////////////////////////////////////////////////////////
258 int DiffDay(sDate& d1, sDate& d2)
259 {
260 tm time1, time2;
261 memset(&time1, 0, sizeof(time1));
262 memset(&time2, 0, sizeof(time2));
263
264 time1.tm_year = d1.year - 1900;
265 time1.tm_mon = d1.month - 1;
266 time1.tm_mday = d1.day;
267
268 time2.tm_year = d2.year - 1900;
269 time2.tm_mon = d2.month - 1;
270 time2.tm_mday = d2.day;
271
272 time_t t1 = mktime(&time1);
273 time_t t2 = mktime(&time2);
274
275 return (int)(difftime(t1, t2)+SECOND_PER_DAY-1)/SECOND_PER_DAY;
276 }
277
278 int DateTimeDiffDay(sDateTime& d1, sDateTime& d2)
279 {
280 tm time1, time2;
281 memset(&time1, 0, sizeof(time1));
282 memset(&time2, 0, sizeof(time2));
283
284 time1.tm_year = d1.year - 1900;
285 time1.tm_mon = d1.month - 1;
286 time1.tm_mday = d1.day;
287
288 time2.tm_year = d2.year - 1900;
289 time2.tm_mon = d2.month - 1;
290 time2.tm_mday = d2.day;
291
292 time_t t1 = mktime(&time1);
293 time_t t2 = mktime(&time2);
294
295 return (int)(difftime(t1, t2)+SECOND_PER_DAY-1)/SECOND_PER_DAY;
296 }
297
298 int DateTimeDiffHour(sDateTime& d1, sDateTime& d2)
299 {
300 tm time1, time2;
301 memset(&time1, 0, sizeof(time1));
302 memset(&time2, 0, sizeof(time2));
303
304 time1.tm_year = d1.year - 1900;
305 time1.tm_mon = d1.month - 1;
306 time1.tm_mday = d1.day;
307 time1.tm_hour = d1.hour;
308
309 time2.tm_year = d2.year - 1900;
310 time2.tm_mon = d2.month - 1;
311 time2.tm_mday = d2.day;
312 time2.tm_hour = d2.hour;
313
314 time_t t1 = mktime(&time1);
315 time_t t2 = mktime(&time2);
316
317 return (int)(difftime(t1, t2)+SECOND_PER_HOUR - 1)/SECOND_PER_HOUR;
318 }
319
320 int DateTimeDiffMiniute(sDateTime& d1, sDateTime& d2)
321 {
322 tm time1, time2;
323 memset(&time1, 0, sizeof(time1));
324 memset(&time2, 0, sizeof(time2));
325
326 time1.tm_year = d1.year - 1900;
327 time1.tm_mon = d1.month - 1;
328 time1.tm_mday = d1.day;
329 time1.tm_hour = d1.hour;
330 time1.tm_min = d1.minute;
331
332 time2.tm_year = d2.year - 1900;
333 time2.tm_mon = d2.month - 1;
334 time2.tm_mday = d2.day;
335 time2.tm_hour = d2.hour;
336 time2.tm_min = d2.minute;
337
338 time_t t1 = mktime(&time1);
339 time_t t2 = mktime(&time2);
340
341 return (int)(difftime(t1, t2)+SECOND_PER_MIN-1)/SECOND_PER_MIN;
342 }
343
下载代码:
DataTime.rar