Debian Codegraph @

CorsixTH/Lua/date.lua

raw
1 --[[ Copyright (c) 2018 Pavel "sofo" Schoffer
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --[[
22 Date class is meant to encapsulate logic around months, years, days, hours.
23 It should be able to do adjustments with regards to days in given month and so on.
24
25 Date objects should be immutable and that is why date adjustments return new objects
26 instead of modifying the current one.
27 ]]
28
29 class "Date"
30
31 ---@type Date
32 local Date = _G["Date"]
33
34 local month_length = {
35 31, -- Jan
36 28, -- Feb (29 in leap years, but TH doesn't have leap years)
37 31, -- Mar
38 30, -- Apr
39 31, -- May
40 30, -- Jun
41 31, -- Jul
42 31, -- Aug
43 30, -- Sep
44 31, -- Oct
45 30, -- Nov
46 31, -- Dec
47 }
48
49 local hours_per_day = 50
50
51 -- STATIC
52
53 --[[ Method returns the number of game hours in one day
54
55 ! This should be use everywhere we query this information
56
57 !return (number) number of hours in a day.
58 ]]
59 function Date.hoursPerDay()
60 return hours_per_day
61 end
62
63 --[[ Method that returns the number of days of a month.
64 --
65 --!param month (1..12) Month being queried.
66 --!return Number of days in the queried month,
67 ]]
68 function Date.daysPerMonth(month)
69 assert(month >= 1 and month <= 12,
70 "Month " .. tostring(month) .. " is not a valid month.")
71 return month_length[month]
72 end
73
74 -- PUBLIC
75
76 --[[ Date constructor
77
78 ! Date is initialized by setting year, month day and hour in this order
79 the default day is 1-01-01T00 - if any of the components isn't set the
80 default value is used.
81
82 !param year (number) year of the new date
83 !param month (number) month of the new date
84 !param day (number) day of the new date
85 !param hour (number) hour of the new date
86
87 !return (Date) created object.
88 ]]
89 function Date:Date(year, month, day, hour)
90 self._year = year or 1
91 self._month = month or 1
92 self._day = day or 1
93 self._hour = hour or 0
94 self:_adjustOverflow()
95 end
96
97 --[[ Returns the last day of the current month
98
99 ! This method finds the correct last day for current month of self. This number
100 is also the number of days within a month. Ignores leap years.
101
102 !return (number)
103 ]]
104 function Date:lastDayOfMonth()
105 return month_length[self._month]
106 end
107
108 --[[ Adds months
109
110 ! Creates a copy of self with adjusted months.
111
112 !param increment (number) number to adjust, can be negative
113
114 !return (Date)
115 ]]
116 function Date:plusMonths(increment)
117 local new_month = self._month + increment
118 return Date(self._year, new_month, self._day, self._hour)
119 end
120
121 --[[ Adds days
122
123 ! Creates a copy of self with adjusted days.
124
125 !param increment (number) number to adjust, can be negative
126
127 !return (Date)
128 ]]
129 function Date:plusDays(increment)
130 local new_day = self._day + increment
131 return Date(self._year, self._month, new_day, self._hour)
132 end
133
134 --[[ Adds years
135
136 ! Creates a copy of self with adjusted years.
137
138 !param increment (number) number to adjust, can be negative
139
140 !return (Date)
141 ]]
142 function Date:plusYears(increment)
143 local new_year = self._year + increment
144 return Date(new_year, self._month, self._day, self._hour)
145 end
146
147 --[[ Adds hours
148
149 ! Creates a copy of self with adjusted hours.
150
151 !param increment (number) number to adjust, can be negative
152
153 !return (Date)
154 ]]
155 function Date:plusHours(increment)
156 local new_hour = self._hour + increment
157 return Date(self._year, self._month, self._day, new_hour)
158 end
159
160 --[[ Returns the month of year
161
162 ! Finds out what is a month of this year (1-12)
163
164 !return (number)
165 ]]
166 function Date:monthOfYear()
167 return self._month
168 end
169
170 --[[ Returns the day of month
171
172 ! Finds out what is a current day in a date (1-31)
173
174 !return (number)
175 ]]
176 function Date:dayOfMonth()
177 return self._day
178 end
179
180 --[[ Returns the year
181
182 ! Finds out what is a current year of this date (1-X)
183
184 !return (number)
185 ]]
186 function Date:year()
187 return self._year
188 end
189
190 --[[ Returns the hour of the day
191
192 ! Finds out what is an hour of this date starting on 0
193
194 !return (number)
195 ]]
196 function Date:hourOfDay()
197 return self._hour
198 end
199
200 --[[ Return string representation
201
202 ! Returns string representation of the date in format y-mm-ddThh
203
204 !return (string)
205 ]]
206 function Date:tostring()
207 return string.format("%d-%02d-%02dT%02d", self._year, self._month, self._day, self._hour)
208 end
209
210 --[[ Checks if date is a last day of a month
211
212 ! Finds out if the current day is a last day in current month with respect to
213 different month lengths.
214
215 !return (boolean)
216 ]]
217 function Date:isLastDayOfMonth()
218 return self._day == self:lastDayOfMonth()
219 end
220
221 --[[ Checks if date is a last day of a year
222
223 ! Finds out if the current day is a last day in current month and current month
224 is a last month in a year.
225
226 !return (boolean)
227 ]]
228 function Date:isLastDayOfYear()
229 return self:isLastDayOfMonth() and self._month == 12
230 end
231
232 --[[ Returns the month of the game
233
234 ! Returns the number of months started since the start of the game. This
235 converts all the years to months and add them together with the started
236 months.
237
238 !return (number)
239 ]]
240 function Date:monthOfGame()
241 return (self._year - 1) * 12 + self._month
242 end
243
244 --[[ Clone the date
245
246 ! Creates another instance of date with a same value
247
248 !return (Date)
249 ]]
250 function Date:clone()
251 return Date(self._year, self._month, self._day, self._hour)
252 end
253
254 --[[ Checks the date
255
256 ! Checks the date with another passed as a parameter, but ignores time
257
258 !param other (Date) The other day to be compared
259
260 !return (boolean)
261 ]]
262 function Date:isSameDay(other)
263 return self._year == other._year and self._month == other._month and self._day == other._day
264 end
265
266 -- METAMETHODS
267
268 local Date_mt = Date._metatable
269
270 function Date_mt.__eq(one, other)
271 -- Checking for class instance crashes with infinite recusrion in Lua 5.3
272 if one.__eq ~= other.__eq then return false end
273
274 return one:isSameDay(other) and one._hour == other._hour
275 end
276
277 function Date_mt.__lt(one, other)
278 -- Checking for class instance crashes with infinite recusrion in Lua 5.3
279 if one.__eq ~= other.__eq then return false end
280
281 if one._year ~= other._year then return one._year < other._year end
282 if one._month ~= other._month then return one._month < other._month end
283 if one._day ~= other._day then return one._day < other._day end
284 return one._hour < other._hour
285 end
286
287 function Date_mt.__le(one, other)
288 -- Checking for class instance crashes with infinite recusrion in Lua 5.3
289 if one.__eq ~= other.__eq then return false end
290
291 if one._year ~= other._year then return one._year < other._year end
292 if one._month ~= other._month then return one._month < other._month end
293 if one._day ~= other._day then return one._day < other._day end
294 return one._hour <= other._hour
295 end
296
297 -- PRIVATE
298
299 --[[ PRIVATE Adjusts the hours overflows
300
301 ! Method to deal with hour being more or less than valid value
302 ]]
303 function Date:_adjustHoursOverflow()
304 while self._hour < 0 do
305 self._hour = self._hour + hours_per_day
306 self._day = self._day - 1
307 end
308 while self._hour >= hours_per_day do
309 self._hour = self._hour - hours_per_day
310 self._day = self._day + 1
311 end
312 self:_adjustDayOverflow()
313 end
314
315 --[[ PRIVATE Adjusts the days overflows
316
317 ! Method to deal with day being more or less than valid value
318 ]]
319 function Date:_adjustDayOverflow()
320 while self._day < 1 do
321 self._month = self._month - 1
322 self:_adjustMonthOverflow()
323 self._day = self._day + self:lastDayOfMonth()
324 end
325 while self._day > self:lastDayOfMonth() do
326 self._day = self._day - self:lastDayOfMonth()
327 self._month = self._month + 1
328 self:_adjustMonthOverflow()
329 end
330 end
331
332 --[[ PRIVATE Adjusts the months overflows
333
334 ! Method to deal with month being more or less than valid value
335 ]]
336 function Date:_adjustMonthOverflow()
337 local monthIx = self._month - 1
338 self._year = self._year + math.floor(monthIx / 12)
339 self._month = monthIx % 12 + 1
340 end
341
342 --[[ PRIVATE Adjusts all the overflows
343
344 ! Normalize date to fix all the overflows of hours, days and months. This
345 method is a key to date adjustments.
346 ]]
347 function Date:_adjustOverflow()
348 self:_adjustMonthOverflow()
349 self:_adjustDayOverflow()
350 self:_adjustHoursOverflow()
351 end