aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic2/Date.hpp
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-04-23 20:49:01 +0200
committer Hop311 <hop3114@gmail.com>2023-04-23 20:49:01 +0200
commitd3f3187209cb4085f27f95ce8ad2a77af25704fd (patch)
tree60971db586e78761341f2b48110d149b1ba0db9d /extension/src/openvic2/Date.hpp
parent1084a5d64df5d3465ef90b3b85fe3374636a3fe8 (diff)
C++ refactoring + simulation prototype
Diffstat (limited to 'extension/src/openvic2/Date.hpp')
-rw-r--r--extension/src/openvic2/Date.hpp139
1 files changed, 68 insertions, 71 deletions
diff --git a/extension/src/openvic2/Date.hpp b/extension/src/openvic2/Date.hpp
index 9ed5963..b19602b 100644
--- a/extension/src/openvic2/Date.hpp
+++ b/extension/src/openvic2/Date.hpp
@@ -1,86 +1,83 @@
#pragma once
#include <cstdint>
-#include <cstddef>
-#include <tuple>
#include <string>
-#include <iostream>
+#include <ostream>
namespace OpenVic2 {
- //A relative period between points in time, measured in days
+ // 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);
-
+ using day_t = int64_t;
+ private:
+ day_t days;
+ public:
+ Timespan(day_t value = 0);
+
+ bool operator<(Timespan other) const;
+ bool operator>(Timespan other) const;
+ bool operator<=(Timespan other) const;
+ bool operator>=(Timespan other) const;
+ bool operator==(Timespan other) const;
+ bool operator!=(Timespan other) const;
+
+ Timespan operator+(Timespan other) const;
+ Timespan operator-(Timespan other) const;
+ Timespan operator*(day_t factor) const;
+ Timespan operator/(day_t factor) const;
+ Timespan& operator+=(Timespan other);
+ Timespan& operator-=(Timespan other);
+ Timespan& operator++();
+ Timespan operator++(int);
+
+ explicit operator day_t() const;
+ explicit operator double() const;
explicit operator std::string() const;
- friend std::ostream& operator<< (std::ostream& out, Timespan const& timespan);
};
+ std::ostream& operator<< (std::ostream& out, Timespan timespan);
- 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
+ // 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);
+ using year_t = uint16_t;
+ using month_t = uint8_t;
+ using day_t = uint8_t;
+
+ static constexpr Timespan::day_t MONTHS_IN_YEAR = 12;
+ static constexpr Timespan::day_t DAYS_IN_YEAR = 365;
+ static constexpr Timespan::day_t DAYS_IN_MONTH[MONTHS_IN_YEAR] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+ static constexpr Timespan::day_t DAYS_UP_TO_MONTH[MONTHS_IN_YEAR] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
+ private:
+ // Number of days since Jan 1st, Year 0
+ Timespan timespan;
+
+ static Timespan _dateToTimespan(year_t year, month_t month, day_t day);
+ public:
+ // The Timespan is considered to be the number of days since Jan 1st, Year 0
+ Date(Timespan total_days);
+ // Year month day specification
+ Date(year_t year = 0, month_t month = 1, day_t day = 1);
+
+ year_t getYear() const;
+ month_t getMonth() const;
+ day_t getDay() const;
+
+ bool operator<(Date other) const;
+ bool operator>(Date other) const;
+ bool operator<=(Date other) const;
+ bool operator>=(Date other) const;
+ bool operator==(Date other) const;
+ bool operator!=(Date other) const;
+
+ Date operator+(Timespan other) const;
+ Timespan operator-(Date other) const;
+ Date& operator+=(Timespan other);
+ Date& operator-=(Timespan other);
+ Date& operator++();
+ Date operator++(int);
explicit operator std::string() const;
- friend std::ostream& operator<< (std::ostream& out, Date const& date);
+ // Parsed from string of the form YYYY.MM.DD
+ static Date from_string(std::string const& date);
};
+ std::ostream& operator<< (std::ostream& out, Date date);
}