diff options
author | markomitos <markomitosevic9@gmail.com> | 2023-03-01 23:20:58 +0100 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-04-14 21:46:32 +0200 |
commit | 48558fdfec509942515b0e2d92e3f1357b201400 (patch) | |
tree | 2c49198c3cbbbb575dc14795132a20c7b9267a2c /extension/src/openvic2 | |
parent | 436b038c1806e326ff6458f1692e9009d3a54346 (diff) |
Add Date to extension
For absolute simulation dates
Add TimeSpan to extension
For relative simulation time
Add Simulation Speed Management to extension
Add Visual Studio directory to .gitignore
Co-authored-by: ClarkeCode <clarke.john.robert@gmail.com>
Co-authored-by: Hop311 <hop3114@gmail.com>
Diffstat (limited to 'extension/src/openvic2')
-rw-r--r-- | extension/src/openvic2/Date.cpp | 108 | ||||
-rw-r--r-- | extension/src/openvic2/Date.hpp | 83 |
2 files changed, 191 insertions, 0 deletions
diff --git a/extension/src/openvic2/Date.cpp b/extension/src/openvic2/Date.cpp new file mode 100644 index 0000000..3c5b8d8 --- /dev/null +++ b/extension/src/openvic2/Date.cpp @@ -0,0 +1,108 @@ +#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; + } + + 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); + } + + //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; + + if (timespan >= 0) { + year = timespan.days / DAYS_IN_YEAR; + int64_t remainingDays = timespan.days % DAYS_IN_YEAR; + + for (size_t x = 0; x < MONTHS_IN_YEAR && remainingDays >= DAYS_IN_MONTH[x]; x++) { + remainingDays -= DAYS_IN_MONTH[x]; + month++; + } + + //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(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); + } + + 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; } + + 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::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); + } +} diff --git a/extension/src/openvic2/Date.hpp b/extension/src/openvic2/Date.hpp new file mode 100644 index 0000000..841b118 --- /dev/null +++ b/extension/src/openvic2/Date.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include <cstdint> +#include <cstddef> +#include <tuple> +#include <string> +#include <iostream> + +namespace OpenVic2 { + //A relative period between points in time, measured in days + struct Timespan { + int64_t days; + + Timespan() : days(0) {} + Timespan(int64_t const& value) : days(value) {} + + bool operator< (Timespan const& other) const; + bool operator> (Timespan const& other) const; + bool operator<= (Timespan const& other) const; + bool operator>= (Timespan const& other) const; + bool operator== (Timespan const& other) const; + bool operator!= (Timespan const& other) const; + + Timespan operator+ (Timespan const& other) const; + Timespan operator- (Timespan const& other) const; + Timespan operator* (int64_t const& factor) const; + Timespan operator/ (int64_t const& factor) const; + + Timespan& operator+= (Timespan const& other); + Timespan& operator-= (Timespan const& other); + }; + + static constexpr size_t MONTHS_IN_YEAR = 12; + static constexpr size_t DAYS_IN_YEAR = 365; + static constexpr size_t DAYS_IN_MONTH[MONTHS_IN_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + using date_t = uint8_t; + using month_t = uint8_t; + using year_t = uint16_t; + using YearMonthDayBundle = std::tuple<year_t, month_t, date_t>; + + //Represents an in-game date + //Note: Current implementation does not account for leap-years, or dates before Year 0 + struct Date { + private: + YearMonthDayBundle gregorianDate; + //Number of days since Jan 1st, Year 0 + Timespan ts; + + public: + //The Timespan is considered to be the number of days since Jan 1st, Year 0 + Date(Timespan const& timespan); + + //Year month day specification + Date(year_t year = 1836, month_t month = 1, date_t day = 1); + + private: + void updateDate(Timespan const& timespan); + + public: + size_t getDay() const; + size_t getMonth() const; + size_t getYear() const; + + bool operator< (Date const& other) const; + bool operator> (Date const& other) const; + bool operator<= (Date const& other) const; + bool operator>= (Date const& other) const; + bool operator== (Date const& other) const; + bool operator!= (Date const& other) const; + + Date operator+ (Timespan timespan) const; + Timespan operator- (Date const& other) const; + + Date& operator+= (Timespan const& timespan); + Date& operator-= (Timespan const& timespan); + //Postfix increment + Date operator++ (int); + + explicit operator std::string() const; + friend std::ostream& operator<< (std::ostream& out, Date const& date); + }; +} |