diff options
author | Hop311 <Hop3114@gmail.com> | 2023-04-18 19:55:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-18 19:55:10 +0200 |
commit | ea077c8a7c78477bd247c7fbd21de13bcf2285e9 (patch) | |
tree | 0cc2be6ef3ab1f2af5c2806f60abe988ca6aa7b5 /extension/src/openvic2/Date.cpp | |
parent | 258a088018d36e987b3ffe4a9b418a6c21ad9217 (diff) | |
parent | 1fdd198f943a41468b03b2cdc62c24147f707239 (diff) |
Merge pull request #88 from OpenVic2Project/map-drawing
Further Map Stuff
Diffstat (limited to 'extension/src/openvic2/Date.cpp')
-rw-r--r-- | extension/src/openvic2/Date.cpp | 181 |
1 files changed, 94 insertions, 87 deletions
diff --git a/extension/src/openvic2/Date.cpp b/extension/src/openvic2/Date.cpp index 3c5b8d8..e1cef72 100644 --- a/extension/src/openvic2/Date.cpp +++ b/extension/src/openvic2/Date.cpp @@ -1,108 +1,115 @@ #include <sstream> #include "Date.hpp" -namespace OpenVic2 { - bool Timespan::operator< (Timespan const& other) const { return days < other.days; } - bool Timespan::operator> (Timespan const& other) const { return days > other.days; } - bool Timespan::operator<= (Timespan const& other) const { return days <= other.days; } - bool Timespan::operator>= (Timespan const& other) const { return days >= other.days; } - bool Timespan::operator== (Timespan const& other) const { return days == other.days; } - bool Timespan::operator!= (Timespan const& other) const { return days != other.days; } - - Timespan Timespan::operator+ (Timespan const& other) const { return Timespan(days + other.days); } - Timespan Timespan::operator- (Timespan const& other) const { return Timespan(days - other.days); } - Timespan Timespan::operator* (int64_t const& factor) const { return Timespan(days * factor); } - Timespan Timespan::operator/ (int64_t const& factor) const { return Timespan(days / factor); } - - Timespan& Timespan::operator+= (Timespan const& other) { - days += other.days; - return *this; - } - Timespan& Timespan::operator-= (Timespan const& other) { - days -= other.days; - return *this; - } +using namespace OpenVic2; + +bool Timespan::operator< (Timespan const& other) const { return days < other.days; } +bool Timespan::operator> (Timespan const& other) const { return days > other.days; } +bool Timespan::operator<= (Timespan const& other) const { return days <= other.days; } +bool Timespan::operator>= (Timespan const& other) const { return days >= other.days; } +bool Timespan::operator== (Timespan const& other) const { return days == other.days; } +bool Timespan::operator!= (Timespan const& other) const { return days != other.days; } + +Timespan Timespan::operator+ (Timespan const& other) const { return Timespan(days + other.days); } +Timespan Timespan::operator- (Timespan const& other) const { return Timespan(days - other.days); } +Timespan Timespan::operator* (int64_t const& factor) const { return Timespan(days * factor); } +Timespan Timespan::operator/ (int64_t const& factor) const { return Timespan(days / factor); } + +Timespan& Timespan::operator+= (Timespan const& other) { + days += other.days; + return *this; +} +Timespan& Timespan::operator-= (Timespan const& other) { + days -= other.days; + return *this; +} - Timespan fromYearZero(year_t year, month_t month, date_t day) { - int64_t daysElapsed = year * DAYS_IN_YEAR; - size_t daysSinceMonthStart = (day == 0) ? 0 : day - 1; //Underflow protection - for (size_t x = 0; x < month && x < MONTHS_IN_YEAR; x++) { - daysElapsed += DAYS_IN_MONTH[x]; - } - daysElapsed += daysSinceMonthStart; - return Timespan(daysElapsed); - } +Timespan::operator std::string() const { + return std::to_string(days); +} +std::ostream &operator<<(std::ostream &out, Timespan const& timespan) { + return out << static_cast<std::string>(timespan); +} - //This function is not set up to handle dates before Year 0 - YearMonthDayBundle toGregorianDate(Timespan const& timespan) { - year_t year = 0; - month_t month = 0; - date_t day = 0; +Timespan fromYearZero(year_t year, month_t month, date_t day) { + int64_t daysElapsed = year * DAYS_IN_YEAR; + size_t daysSinceMonthStart = (day == 0) ? 0 : day - 1; //Underflow protection + for (size_t x = 0; x < month && x < MONTHS_IN_YEAR; x++) { + daysElapsed += DAYS_IN_MONTH[x]; + } + daysElapsed += daysSinceMonthStart; + return Timespan(daysElapsed); +} - if (timespan >= 0) { - year = timespan.days / DAYS_IN_YEAR; - int64_t remainingDays = timespan.days % DAYS_IN_YEAR; +//This function is not set up to handle dates before Year 0 +YearMonthDayBundle toGregorianDate(Timespan const& timespan) { + year_t year = 0; + month_t month = 0; + date_t day = 0; - for (size_t x = 0; x < MONTHS_IN_YEAR && remainingDays >= DAYS_IN_MONTH[x]; x++) { - remainingDays -= DAYS_IN_MONTH[x]; - month++; - } + if (timespan >= 0) { + year = timespan.days / DAYS_IN_YEAR; + int64_t remainingDays = timespan.days % DAYS_IN_YEAR; - //Corrects month and day to be 1-indexed + for (size_t x = 0; x < MONTHS_IN_YEAR && remainingDays >= DAYS_IN_MONTH[x]; x++) { + remainingDays -= DAYS_IN_MONTH[x]; month++; - day++; } - return std::make_tuple(year, month, day); + + //Corrects month and day to be 1-indexed + month++; + day++; } + return std::make_tuple(year, month, day); +} - Date::Date(Timespan const& timespan) : ts(timespan) { updateDate(ts); } +Date::Date(Timespan const& timespan) : ts(timespan) { updateDate(ts); } - Date::Date(year_t year, month_t month, date_t day) { - ts = fromYearZero(year, month, day); - updateDate(ts); - } +Date::Date(year_t year, month_t month, date_t day) { + ts = fromYearZero(year, month, day); + updateDate(ts); +} - void Date::updateDate(Timespan const& timespan) { - gregorianDate = toGregorianDate(timespan); - } +void Date::updateDate(Timespan const& timespan) { + gregorianDate = toGregorianDate(timespan); +} - size_t Date::getDay() const { return std::get<2>(gregorianDate); } - size_t Date::getMonth() const { return std::get<1>(gregorianDate); } - size_t Date::getYear() const { return std::get<0>(gregorianDate); } +size_t Date::getDay() const { return std::get<2>(gregorianDate); } +size_t Date::getMonth() const { return std::get<1>(gregorianDate); } +size_t Date::getYear() const { return std::get<0>(gregorianDate); } - bool Date::operator< (Date const& other) const { return ts < other.ts; } - bool Date::operator> (Date const& other) const { return ts > other.ts; } - bool Date::operator<= (Date const& other) const { return ts <= other.ts; } - bool Date::operator>= (Date const& other) const { return ts >= other.ts; } - bool Date::operator== (Date const& other) const { return ts == other.ts; } - bool Date::operator!= (Date const& other) const { return ts != other.ts; } +bool Date::operator< (Date const& other) const { return ts < other.ts; } +bool Date::operator> (Date const& other) const { return ts > other.ts; } +bool Date::operator<= (Date const& other) const { return ts <= other.ts; } +bool Date::operator>= (Date const& other) const { return ts >= other.ts; } +bool Date::operator== (Date const& other) const { return ts == other.ts; } +bool Date::operator!= (Date const& other) const { return ts != other.ts; } - Date Date::operator+ (Timespan timespan) const { return Date(ts + timespan); } - Timespan Date::operator- (Date const& other) const { return ts - other.ts; } +Date Date::operator+ (Timespan timespan) const { return Date(ts + timespan); } +Timespan Date::operator- (Date const& other) const { return ts - other.ts; } - Date& Date::operator+= (Timespan const& timespan) { - ts += timespan; - updateDate(ts); - return *this; - } - Date& Date::operator-= (Timespan const& timespan) { - ts -= timespan; - updateDate(ts); - return *this; - } - Date Date::operator++ (int) { - Date oldCopy = *this; - (*this) += 1; - return oldCopy; - } +Date& Date::operator+= (Timespan const& timespan) { + ts += timespan; + updateDate(ts); + return *this; +} +Date& Date::operator-= (Timespan const& timespan) { + ts -= timespan; + updateDate(ts); + return *this; +} +Date Date::operator++ (int) { + Date oldCopy = *this; + (*this) += 1; + return oldCopy; +} - Date::operator std::string() const { - std::stringstream ss; - ss << getYear() << '.' << getMonth() << '.' << getDay(); - return ss.str(); - } - std::ostream &operator<<(std::ostream &out, Date const& date) { - return out << static_cast<std::string>(date); - } +Date::operator std::string() const { + std::stringstream ss; + ss << getYear() << '.' << getMonth() << '.' << getDay(); + return ss.str(); +} +std::ostream &operator<<(std::ostream &out, Date const& date) { + return out << static_cast<std::string>(date); } |