aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic2/Date.hpp
blob: 9ed5963deffd527e6b80b4eca8c0a55a88c1360c (plain) (blame)
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
#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);

      explicit operator std::string() const;
      friend std::ostream& operator<< (std::ostream& out, Timespan const& 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
   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);
   };
}