-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Date.cpp
185 lines (150 loc) · 3.12 KB
/
Date.cpp
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
#include "Date.h"
#include "OSCompatibilityLayer.h"
#include "StringUtils.h"
#include <array>
#include <vector>
namespace
{
const std::array days_by_month{
0, // January
31, // February
59, // March
90, // April
120, // May
151, // June
181, // July
212, // August
243, // September
273, // October
304, // November
334, // December
365, // End of year
};
} // namespace
int CommonItems::DaysInMonth(int month)
{
return days_by_month[month] - days_by_month[month - 1];
}
std::vector<std::string> getDateElementStrings(const std::string& s)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, '.'))
{
tokens.push_back(token);
}
return tokens;
}
date::date(std::string init, const bool AUC)
{
init = commonItems::remQuotes(init);
std::vector<std::string> dateElements = getDateElementStrings(init);
try
{
if (dateElements.size() >= 3)
{
year = std::stoi(dateElements[0]);
month = std::stoi(dateElements[1]);
day = std::stoi(dateElements[2]);
}
else if (dateElements.size() == 2)
{
year = std::stoi(dateElements[0]);
month = std::stoi(dateElements[1]);
}
else if (dateElements.size() == 1)
{
year = std::stoi(dateElements[0]);
}
else
{
Log(LogLevel::Warning) << "Problem constructing date: at least a year should be provided!";
}
}
catch (std::exception& e)
{
Log(LogLevel::Warning) << "Problem constructing date from string \"" + init + "\": " + e.what() + "!";
}
if (AUC)
{
year = convertAUCtoAD(year);
}
}
void date::ChangeByDays(int days)
{
auto current_day_in_year = calculateDayInYear() + days;
while (current_day_in_year < 0)
{
--year;
current_day_in_year += 365;
}
year += current_day_in_year / 365;
current_day_in_year = current_day_in_year % 365;
for (month = 0; current_day_in_year > days_by_month[month]; ++month)
{
}
if (month > 0)
{
day = current_day_in_year - days_by_month[month - 1];
}
else
{
day = current_day_in_year;
}
}
void date::ChangeByMonths(int months)
{
int new_year = year;
int new_month = month;
new_year += months / 12;
new_month += months % 12;
if (new_month > 12)
{
++new_year;
new_month -= 12;
}
else if (new_month < 1)
{
--new_year;
new_month += 12;
}
year = new_year;
month = new_month;
}
void date::increaseByMonths(const int months)
{
ChangeByMonths(months);
}
int date::convertAUCtoAD(const int yearAUC)
{
auto yearAD = yearAUC - 753;
if (yearAD <= 0)
{
--yearAD;
}
return yearAD;
}
float date::diffInYears(const date& rhs) const
{
auto years = static_cast<float>(year - rhs.year);
years += static_cast<float>(calculateDayInYear() - rhs.calculateDayInYear()) / 365;
return years;
}
std::string date::toString() const
{
std::stringstream buf;
buf << year << '.' << month << '.' << day;
return buf.str();
}
std::ostream& operator<<(std::ostream& out, const date& date)
{
out << date.year << '.' << date.month << '.' << date.day;
return out;
}
int date::calculateDayInYear() const
{
if (month >= 1 && month <= 12)
return day + days_by_month[static_cast<size_t>(month) - 1];
return day;
}