1 //       _________ __                 __
2 //      /   _____//  |_____________ _/  |______     ____  __ __  ______
3 //      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4 //      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
5 //     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6 //             \/                  \/          \//_____/            \/
7 //  ______________________                           ______________________
8 //                        T H E   W A R   B E G I N S
9 //         Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name date.h - The date header file. */
12 //
13 //      (c) Copyright 2018-2019 by Andrettin
14 //
15 //      This program is free software; you can redistribute it and/or modify
16 //      it under the terms of the GNU General Public License as published by
17 //      the Free Software Foundation; only version 2 of the License.
18 //
19 //      This program is distributed in the hope that it will be useful,
20 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //      GNU General Public License for more details.
23 //
24 //      You should have received a copy of the GNU General Public License
25 //      along with this program; if not, write to the Free Software
26 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 //      02111-1307, USA.
28 //
29 
30 #ifndef __DATE_H__
31 #define __DATE_H__
32 
33 //@{
34 
35 /*----------------------------------------------------------------------------
36 --  Includes
37 ----------------------------------------------------------------------------*/
38 
39 #include <string>
40 
41 /*----------------------------------------------------------------------------
42 --  Declarations
43 ----------------------------------------------------------------------------*/
44 
45 class CCalendar;
46 class CTimeline;
47 
48 #define BaseCalendarYearOffsetForHours 10000 //essentially the Human Era
49 
50 class CDate
51 {
52 public:
53 	static CDate FromString(const std::string &date_str);
54 
55 	static unsigned long long CurrentTotalHours;				/// Current total in-game hours, counting from 1 January 10000 BC 00:00
56 
57 	void Clear();
58 	bool ContainsDate(const CDate &date) const;	/// whether this date "contains" another (i.e. if it is subsequent to another, and in an appropriate timeline)
59 	void AddYears(const int years);
60 	void AddMonths(const CCalendar *calendar, const int months);
61 	void AddDays(const CCalendar *calendar, const int days, const int day_multiplier = 1);
62 	void AddHours(const CCalendar *calendar, const long long int hours, const int day_multiplier = 1);
63 	CDate ToCalendar(CCalendar *current_calendar, CCalendar *new_calendar) const;
64 	CDate ToBaseCalendar(CCalendar *current_calendar) const;
65 	std::string ToString(const CCalendar *calendar) const;
66 	std::string ToDisplayString(const CCalendar *calendar, const bool year_only = false) const;
67 	int GetTotalDays(const CCalendar *calendar) const;
68 	unsigned long long GetTotalHours(CCalendar *calendar) const;	/// gets the total amount of hours for the particular calendar in this date, counting from -10,000 in the base calendar
69 	int GetDayOfTheWeek(const CCalendar *calendar) const;			/// gets the day of the week for this date in a given calendar
70 
71 	int Year = 0;
72 	char Month = 1;
73 	char Day = 1;
74 	char Hour = DEFAULT_HOURS_PER_DAY / 2;
75 	CTimeline *Timeline = nullptr;
76 
77 	bool operator <(const CDate &rhs) const {
78 		if (Year < rhs.Year) {
79 			return true;
80         } else if (Year == rhs.Year) {
81 			if (Month < rhs.Month) {
82 				return true;
83 			} else if (Month == rhs.Month) {
84 				if (Day < rhs.Day) {
85 					return true;
86 				} else if (Day == rhs.Day) {
87 					return Hour < rhs.Hour;
88 				}
89 			}
90 		}
91 
92 		return false;
93 	}
94 
95 	bool operator <=(const CDate &rhs) const {
96 		if (Year < rhs.Year) {
97 			return true;
98         } else if (Year == rhs.Year) {
99 			if (Month < rhs.Month) {
100 				return true;
101 			} else if (Month == rhs.Month) {
102 				if (Day < rhs.Day) {
103 					return true;
104 				} else if (Day == rhs.Day) {
105 					return Hour <= rhs.Hour;
106 				}
107 			}
108 		}
109 
110 		return false;
111 	}
112 
113 	bool operator >(const CDate &rhs) const {
114 		if (Year > rhs.Year) {
115 			return true;
116         } else if (Year == rhs.Year) {
117 			if (Month > rhs.Month) {
118 				return true;
119 			} else if (Month == rhs.Month) {
120 				if (Day > rhs.Day) {
121 					return true;
122 				} else if (Day == rhs.Day) {
123 					return Hour > rhs.Hour;
124 				}
125 			}
126 		}
127 
128 		return false;
129 	}
130 
131 	bool operator >=(const CDate &rhs) const {
132 		if (Year > rhs.Year) {
133 			return true;
134         } else if (Year == rhs.Year) {
135 			if (Month > rhs.Month) {
136 				return true;
137 			} else if (Month == rhs.Month) {
138 				if (Day > rhs.Day) {
139 					return true;
140 				} else if (Day == rhs.Day) {
141 					return Hour >= rhs.Hour;
142 				}
143 			}
144 		}
145 
146 		return false;
147 	}
148 
149 	bool operator ==(const CDate &rhs) const {
150 		return Year == rhs.Year && Month == rhs.Month && Day == rhs.Day && Hour == rhs.Hour;
151 	}
152 };
153 
154 /*----------------------------------------------------------------------------
155 --  Functions
156 ----------------------------------------------------------------------------*/
157 
158 extern void SetCurrentDate(const std::string &calendar_ident, const std::string &date_string);
159 extern void SetCurrentDayOfTheWeek(const std::string &calendar_ident, const int day_of_the_week);
160 extern void SetCurrentTotalHours(const unsigned long long hours);
161 
162 //@}
163 
164 #endif // !__DATE_H__
165