-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.cpp
More file actions
239 lines (203 loc) · 5.01 KB
/
Copy pathDate.cpp
File metadata and controls
239 lines (203 loc) · 5.01 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
#include <iomanip>
#include "Date.h"
using namespace std;
Date::Date() {
date= new int[3];
date[0] = 1;
date[1] = 1;
date[2] = 1970;
objectCount++;
}
//Overload
Date::Date(int m, int d, int y)
{
date = new int[3];
if(isValidDate(m, d, y))
{
date[0] = m;
date[1] = d;
date[2] = y;
}
else
{
date[0] = 1;
date[1] = 1;
date[2] = 1970;
}
++objectCount;
}
//j date
Date::Date(int j)
{
date = new int[3];
julianToGregorian(j);
objectiveCount++;
}
Date::Date(const Date& other) {
date = new int[3];
date[0] = other.date[0];
date[1] = other.date[1];
date[2] = other.date[2];
objectCount++;
}
Date::~Date()
{
delete[] date;
date = nullptr;
--objectCount;
}
Date& Date::operator=(const Date& other)
{
if(isValidDate(other.date[0],other.date[1],other.date[2]))
{
date[0] = other.date[0];
date[1] = other.date[1];
date[2] = other.date[2];
}
else
{
date[0] = 1;
date[1] = 1;
date[2] = 1970;
}
return *this;
}
int Date::getMonth() const { return date[0]; }
int Date::getDay() const { return date[1]; }
int Date::getYear() const {return date[2]; }
string Date::getDayName() const {
const std::string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
return days[dayOfWeek()];
}
string Date::getMonthName() const {
const std::string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
return months[date[0] - 1];
}
int Date::julian() const
{
return gregorianToJulian();
}
int Date::count(){
return objectCount;
}
bool Date::isLeapYear(int y) const {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int Date::dayOfWeek() const {
int jd = gregorianToJulian();
return (jd + 1) % 7;
}
bool Date::isValidDate(int m,int d, int y) const
{
if(y < 1 || m < 1 || m > 12 || d < 1)
return false;
return d <= daysInMonth(m,y);
}
int Date::daysInMonth(int m, int y)const {
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(m == 2 && isLeapYear(y))
return 29;
return days[m -1 ];
}
void Date::julianToGregorian(int jd){
int l = jd + 68569;
int n = 4 * l / 146097;
l = l - (146097 * n + 3) / 4;
int i = 4000 * (l + 1) / 1461001;
l = l - 1461 * i / 4 + 31;
int j = 80 * l / 2447;
int k = l - 2447 * j / 80;
l = j / 11;
j = j + 2 - 12 * l;
i = 100 * (n - 49) + i + l;
date[2] = i; // Year
date[0] = j; //
date[1] = k; //idk how to read fortran ima ask someone
}
int Date::gregorianToJulian() const
{
int i = date[2];
int j = date[0];
int k = date[1];
int jd = k - 32075 + 1461 * (i + 4800 + (j - 14) / 12) / 4
+ 367 * (j - 2 - (j - 14) / 12 * 12) / 12
- 3 * ((i + 4900 + (j - 14) / 12) / 100) / 4; // enable wrap
return jd;
}
void Date::addDays(int days)
{
int jd = gregorianToJulian() + days;
julianToGregorian(jd);
}
Date& Date::operator+=(int days)
{
addDays(days);
return *this;
}
Date& Date::operator-=(int days) {
addDays(-days);
return *this;
}
Date Date::operator-(int days) const {
Date temp(*this);
temp.addDays(-days);
return temp;
}
Date Date::operator+(int days)const
{
Date temp(*this);
temp.addDays(days);
return temp;
}
int Date::operator-(const Date& other) const
{
return this->gregorianToJulian() - other.gregorianToJulian();
}
Date& Date::operator++() { // Prefix
addDays(1);
return *this;
}
Date Date::operator++(int) { // Postfix
Date temp(*this);
addDays(1);
return temp;
}
Date& Date::operator--() { // Prefix
addDays(-1);
return *this;
}
Date Date::operator--(int) { // Postfix
Date temp(*this);
addDays(-1);
return temp;
}
bool Date::operator==(const Date& other) const {
return date[0] == other.date[0] &&
date[1] == other.date[1] &&
date[2] == other.date[2];
}
bool Date::operator<(const Date& other) const {
return gregorianToJulian() < other.gregorianToJulian();
}
bool Date::operator>(const Date& other) const {
return gregorianToJulian() > other.gregorianToJulian();
}
bool Date::operator!=(const Date& other) const {
return !(*this == other);
}
bool Date::operator<=(const Date& other) const {
return !(*this > other);
}
bool Date::operator>=(const Date& other) const {
return !(*this < other);
}
// Friend functions
Date operator+(int days, const Date& d) {
return d + days;
}
ostream& operator<<(ostream& os, const Date& d) {
os << d.date[0] << "/" << d.date[1] << "/" << d.date[2];
return os;
}