Skip to content

Commit c56131a

Browse files
committed
Made Date constexpr so that PROPERTY getters can be constexpr
1 parent 268a694 commit c56131a

File tree

4 files changed

+204
-284
lines changed

4 files changed

+204
-284
lines changed

src/openvic-simulation/types/Date.cpp

Lines changed: 0 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,12 @@
11
#include "Date.hpp"
22

3-
#include <algorithm>
4-
#include <cassert>
53
#include <cctype>
6-
#include <charconv>
74

85
#include "openvic-simulation/utility/Logger.hpp"
96
#include "openvic-simulation/utility/StringUtils.hpp"
107

118
using namespace OpenVic;
129

13-
Timespan::Timespan(day_t value) : days { value } {}
14-
15-
bool Timespan::operator<(Timespan other) const {
16-
return days < other.days;
17-
};
18-
bool Timespan::operator>(Timespan other) const {
19-
return days > other.days;
20-
};
21-
bool Timespan::operator<=(Timespan other) const {
22-
return days <= other.days;
23-
};
24-
bool Timespan::operator>=(Timespan other) const {
25-
return days >= other.days;
26-
};
27-
bool Timespan::operator==(Timespan other) const {
28-
return days == other.days;
29-
};
30-
bool Timespan::operator!=(Timespan other) const {
31-
return days != other.days;
32-
};
33-
34-
Timespan Timespan::operator+(Timespan other) const {
35-
return days + other.days;
36-
}
37-
38-
Timespan Timespan::operator-(Timespan other) const {
39-
return days - other.days;
40-
}
41-
42-
Timespan Timespan::operator*(day_t factor) const {
43-
return days * factor;
44-
}
45-
46-
Timespan Timespan::operator/(day_t factor) const {
47-
return days / factor;
48-
}
49-
50-
Timespan& Timespan::operator+=(Timespan other) {
51-
days += other.days;
52-
return *this;
53-
}
54-
55-
Timespan& Timespan::operator-=(Timespan other) {
56-
days -= other.days;
57-
return *this;
58-
}
59-
60-
Timespan& Timespan::operator++() {
61-
days++;
62-
return *this;
63-
}
64-
65-
Timespan Timespan::operator++(int) {
66-
Timespan old = *this;
67-
++(*this);
68-
return old;
69-
}
70-
71-
Timespan::day_t Timespan::to_int() const {
72-
return days;
73-
}
74-
75-
Timespan::operator day_t() const {
76-
return days;
77-
}
78-
7910
std::string Timespan::to_string() const {
8011
return std::to_string(days);
8112
}
@@ -84,145 +15,10 @@ Timespan::operator std::string() const {
8415
return to_string();
8516
}
8617

87-
Timespan Timespan::from_years(day_t num) {
88-
return num * Date::DAYS_IN_YEAR;
89-
}
90-
91-
Timespan Timespan::from_months(day_t num) {
92-
return (num / Date::MONTHS_IN_YEAR) * Date::DAYS_IN_YEAR + Date::DAYS_UP_TO_MONTH[num % Date::MONTHS_IN_YEAR];
93-
}
94-
95-
Timespan Timespan::from_days(day_t num) {
96-
return num;
97-
}
98-
9918
std::ostream& OpenVic::operator<<(std::ostream& out, Timespan const& timespan) {
10019
return out << timespan.to_string();
10120
}
10221

103-
const std::string Date::MONTH_NAMES[MONTHS_IN_YEAR] = {
104-
"January", "February", "March", "April", "May", "June",
105-
"July", "August", "September", "October", "November",
106-
"December"
107-
};
108-
109-
Timespan Date::_date_to_timespan(year_t year, month_t month, day_t day) {
110-
month = std::clamp<month_t>(month, 1, MONTHS_IN_YEAR);
111-
day = std::clamp<day_t>(day, 1, DAYS_IN_MONTH[month - 1]);
112-
return year * DAYS_IN_YEAR + DAYS_UP_TO_MONTH[month - 1] + day - 1;
113-
}
114-
115-
Timespan::day_t const* Date::DAYS_UP_TO_MONTH = generate_days_up_to_month();
116-
117-
Timespan::day_t const* Date::generate_days_up_to_month() {
118-
static Timespan::day_t days_up_to_month[MONTHS_IN_YEAR];
119-
Timespan::day_t days = 0;
120-
int month = 0;
121-
while (month < MONTHS_IN_YEAR) {
122-
days_up_to_month[month] = days;
123-
days += DAYS_IN_MONTH[month++];
124-
}
125-
assert(days == DAYS_IN_YEAR);
126-
return days_up_to_month;
127-
}
128-
129-
Date::month_t const* Date::MONTH_FROM_DAY_IN_YEAR = generate_month_from_day_in_year();
130-
131-
Date::month_t const* Date::generate_month_from_day_in_year() {
132-
static month_t month_from_day_in_year[DAYS_IN_YEAR];
133-
Timespan::day_t days_left = 0;
134-
int day = 0, month = 0;
135-
while (day < DAYS_IN_YEAR) {
136-
days_left = (days_left > 0 ? days_left : DAYS_IN_MONTH[month++]) - 1;
137-
month_from_day_in_year[day++] = month;
138-
}
139-
assert(days_left == 0);
140-
assert(month_from_day_in_year[DAYS_IN_YEAR - 1] == MONTHS_IN_YEAR);
141-
return month_from_day_in_year;
142-
}
143-
144-
Date::Date(Timespan total_days) : timespan { total_days } {
145-
if (timespan < 0) {
146-
Logger::error("Invalid timespan for date: ", timespan, " (cannot be negative)");
147-
timespan = 0;
148-
}
149-
}
150-
151-
Date::Date(year_t year, month_t month, day_t day) : timespan { _date_to_timespan(year, month, day) } {}
152-
153-
Date::year_t Date::get_year() const {
154-
return static_cast<Timespan::day_t>(timespan) / DAYS_IN_YEAR;
155-
}
156-
157-
Date::month_t Date::get_month() const {
158-
return MONTH_FROM_DAY_IN_YEAR[static_cast<Timespan::day_t>(timespan) % DAYS_IN_YEAR];
159-
}
160-
161-
Date::day_t Date::get_day() const {
162-
return (static_cast<Timespan::day_t>(timespan) % DAYS_IN_YEAR) - DAYS_UP_TO_MONTH[get_month() - 1] + 1;
163-
}
164-
165-
bool Date::operator<(Date other) const {
166-
return timespan < other.timespan;
167-
};
168-
bool Date::operator>(Date other) const {
169-
return timespan > other.timespan;
170-
};
171-
bool Date::operator<=(Date other) const {
172-
return timespan <= other.timespan;
173-
};
174-
bool Date::operator>=(Date other) const {
175-
return timespan >= other.timespan;
176-
};
177-
bool Date::operator==(Date other) const {
178-
return timespan == other.timespan;
179-
};
180-
bool Date::operator!=(Date other) const {
181-
return timespan != other.timespan;
182-
};
183-
184-
Date Date::operator+(Timespan other) const {
185-
return timespan + other;
186-
}
187-
188-
Timespan Date::operator-(Date other) const {
189-
return timespan - other.timespan;
190-
}
191-
192-
Date& Date::operator+=(Timespan other) {
193-
timespan += other;
194-
return *this;
195-
}
196-
197-
Date& Date::operator-=(Timespan other) {
198-
timespan -= other;
199-
return *this;
200-
}
201-
202-
Date& Date::operator++() {
203-
timespan++;
204-
return *this;
205-
}
206-
207-
Date Date::operator++(int) {
208-
Date old = *this;
209-
++(*this);
210-
return old;
211-
}
212-
213-
bool Date::in_range(Date start, Date end) const {
214-
return start <= *this && *this <= end;
215-
}
216-
217-
std::string const& Date::get_month_name() const {
218-
const month_t month = get_month();
219-
if (1 <= month && month <= MONTHS_IN_YEAR) {
220-
return MONTH_NAMES[month - 1];
221-
}
222-
static const std::string invalid_month_name = "Invalid Month";
223-
return invalid_month_name;
224-
}
225-
22622
std::string Date::to_string() const {
22723
std::stringstream ss;
22824
ss << *this;

0 commit comments

Comments
 (0)