-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDate.cpp
More file actions
311 lines (288 loc) · 6.77 KB
/
MyDate.cpp
File metadata and controls
311 lines (288 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//File Name: MyDate.cpp
//Author: 李晓鹏
//Student Number: 3016218087
//Email: 1940913015@qq.com
//Assignment:#2
//Version: 1.2
//Description: 该CPP程序用于MyDate.h的实现,通过构造函数来产生新对象,通过一系列函数的调用来改变对象内容,输出要在MAIN函数里用COUT << toString()
/*Future Improvements:
1.每个函数的注释要更加详细
2.要优化代码的总行数,减少冗余代码
3.变量命名要更有意义
4.1752年更换历法还存在一些问题,下一步将优化*/
#include <iostream>
#include <ctime>
#include <cmath>
#include <string>
#include "MyDate.h"
using namespace std;
//北京时区
const static long TIMEZONE_OFFSET = 60 * 60 * 8;
//转换构造函数,秒数转换成天
MyDate::MyDate(long long elapseTime)
{
int day;
day = (elapseTime + TIMEZONE_OFFSET) / 86400;
dayStorage = day;
}
//构造函数,构造一个日期并换成天存储
MyDate::MyDate(int year , int month , int newDay)
{
int day;
if(year >= 1970)
{
day = newDay - 1;
while(month > 1)
{
day = day + getNumberOfDaysInMonth(year, month-1);
month--;
}
while(year > 1970)
{
day = day + (isLeapYear(year) ? 366 : 365);
year--;
}
}
else
{
//考虑1752年9月2日到14日中间少11天
if (year == 1752 && month == 9 && day > 2)
day = newDay - 12;
else
day = newDay - 1;
while(month > 1)
{
day = day + getNumberOfDaysInMonth(year, month-1);
month--;
}
while(year < 1970)
{
if(year == 1752)
{
day = day - (isLeapYear(year) ? 366 : 365) + 11;
year++;
}
else
{
day = day - (isLeapYear(year) ? 366 : 365);
year++;
}
}
}
dayStorage = day;
}
//增加天数
MyDate MyDate::add(int addDay) const
{
int day;
day = dayStorage + addDay;
MyDate dva(1 , day);
return dva;
}
//减少天数
MyDate MyDate::subtract(int subtractDay) const
{
int day;
day = dayStorage - subtractDay;
MyDate dva(1 , day);
return dva;
}
//日期减法得天数
int MyDate::subtract(const MyDate& dva) const
{
int day;
day = this -> dayStorage - dva.dayStorage;
return day;
}
//判断日期是否相等
bool MyDate::equals(const MyDate& dva) const
{
if(this -> dayStorage == dva.dayStorage)
return true;
else
return false;
}
//判断日期是否小于等于
bool MyDate::lessThanOrEquals(const MyDate& dva) const
{
if(this -> dayStorage <= dva.dayStorage)
return true;
else
return false;
}
//判断日期是否大于
bool MyDate::greaterThan(const MyDate& dva) const
{
if(this -> dayStorage > dva.dayStorage)
return true;
else
return false;
}
//判断日期是否大于等于
bool MyDate::greaterThanOrEquals(const MyDate& dva) const
{
if(this -> dayStorage >= dva.dayStorage)
return true;
else
return false;
}
//toString函数,用于输出年月日
string MyDate::toString() const
{
int month;
int year;
int day;
string date1 = "";
string date2 = "";
string date3 = "";
string result = "";
string number = "";
day = dayStorage;
if(day >= 0)
{
year = 1970;
while (day >= (isLeapYear(year) ? 366 : 365))
{
day = day - (isLeapYear(year) ? 366 : 365);
year++;
}
month = 1;
while (day >= getNumberOfDaysInMonth(year, month))
{
day = day - getNumberOfDaysInMonth(year, month);
month++;
}
day = day + 1;
}
else
{
//将负数天数换成正数来减
day = fabs(day) ;
year = 1970;
while (day >= (isLeapYear(year-1) ? 366 : 365))
{
if(year == 1753)
{
day = day - (isLeapYear(year-1) ? 366 : 365) + 11;
year--;
}
else
{
day = day - (isLeapYear(year-1) ? 366 : 365);
year--;
}
}
if (year == 1753 && day >= 355 && day <366)
{
day = day - 355;
year--;
}
if (day > 0)
{
//当需要减去的天数不够一年时,先减去1天变成前一年最后一天的年月
year--;
month = 12;
day = day - 1;
while (day >= getNumberOfDaysInMonth(year, month))
{
day = day - getNumberOfDaysInMonth(year, month);
month--;
}
if (year == 1752 && month == 9 && day <= 16)
day = 30 - day ;
else
day = getNumberOfDaysInMonth(year, month) - day;
}
else
{
//需要减去的天数为零时,正好是这一年的一月一日
month = 1;
day = 1;
}
}
//年月日按标准格式转换
//年
if (year >= 0)
{
number = year % 10 + '0';
date1 = number + date1;
while (year / 10 != 0)
{
year = year / 10;
number = year % 10 + '0';
date1 = number + date1;
}
while (date1.length() < 4)
date1 = '0' + date1;
}
else
{
year = fabs(year);
number = year % 10 + '0';
date1 = number + date1;
while (year / 10 != 0)
{
year = year / 10;
number = year % 10 + '0';
date1 = number + date1;
}
while (date1.length() < 4)
date1 = '0' + date1;
date1 = "-" + date1;
}
//月
number = month % 10 + '0';
date2 = number + date2;
if (month / 10 != 0)
{
month = month / 10;
number = month % 10 + '0';
date2 = number + date2;
}
else
date2 = '0' + date2;
date2 = "-" + date2;
//日
number = day % 10 + '0';
date3 = number + date3;
if (day / 10 != 0)
{
day = day / 10;
number = day % 10 + '0';
date3 = number + date3;
}
else
date3 = '0' + date3;
date3 = "-" + date3;
//总结果
result = date1 + date2 + date3;
return result;
}
//用于类内部更改私有的day
MyDate::MyDate (int mercy , int day)
{
dayStorage = day;
}
int dayStorage;
//判断是否是闰年
bool MyDate::isLeapYear(int year) const
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
int MyDate::getNumberOfDaysInMonth(int year, int month) const
{
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 11)
return 30;
//考虑1752年9月的特殊19天,将9月单独讨论
if (year == 1752 && month == 9)
return 19;
else if (year != 1752 && month == 9)
return 30;
else
if (month == 2)
return isLeapYear(year) ? 29 : 28;
return 0;
}