UFO: Alien Invasion
Doxygen documentation generating
DateTime.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2023 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24 
25 #include "DateTime.h"
26 
27 /* Constructors */
28 
34 DateTime::DateTime (int day, int second) :
35  day(day),
36  second(second)
37 {
38  reCalculate();
39 }
40 
41 /* public methods */
42 
47 {
48  return day;
49 }
50 
55 {
56  return second;
57 }
58 
59 /* Operators */
60 
67 {
68  this->day += other.getDateAsDays();
69  this->second += other.getTimeAsSeconds();
70  this->reCalculate();
71  return *this;
72 }
73 
80 {
81  return DateTime(*this) += other;
82 }
83 
90 {
91  this->day -= other.getDateAsDays();
92  this->second -= other.getTimeAsSeconds();
93  this->reCalculate();
94  return *this;
95 }
96 
103 {
104  return DateTime(*this) -= other;
105 }
106 
110 bool DateTime::operator==(const DateTime& other) const
111 {
112  return this->day == other.getDateAsDays() && this->second == other.getTimeAsSeconds();
113 }
114 
118 bool DateTime::operator!=(const DateTime& other) const
119 {
120  return !(*this == other);
121 }
122 
126 bool DateTime::operator<(const DateTime& other) const
127 {
128  return (this->day < other.getDateAsDays()) || (this->day == other.getDateAsDays() && this->second < other.getTimeAsSeconds());
129 }
130 
134 bool DateTime::operator<=(const DateTime& other) const
135 {
136  return (*this == other) || (*this < other);
137 }
138 
142 bool DateTime::operator>(const DateTime& other) const
143 {
144  return (this->day > other.getDateAsDays()) || (this->day == other.getDateAsDays() && this->second > other.getTimeAsSeconds());
145 }
146 
150 bool DateTime::operator>=(const DateTime& other) const
151 {
152  return (*this == other) || (*this > other);
153 }
154 
155 /* Internal methods */
156 
161 {
162  int day_difference = int(second / SECONDS_PER_DAY);
163  if (day_difference != 0) {
164  day += day_difference;
165  second -= day_difference * SECONDS_PER_DAY;
166  }
167  if (second < 0) {
168  day--;
170  }
171 }
172 
173 #if 0
174 
175 #include "../shared/shared.h"
176 #include <math.h>
177 
178 /* Public methods */
179 static const short DAYS_PER_MONTH[DateTime::MONTHS_PER_YEAR] = {
180  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
181 };
191 void DateTime::getDateParts (int* year, short* month, short* day, short* hour, short* minute, short* second) const
192 {
193  *year = int(this->day / DAYS_PER_YEAR);
194  *month = 1;
195  *day = 1 + this->day % DAYS_PER_YEAR;
196  while ((*day) >= DAYS_PER_MONTH[(*month) - 1]) {
197  (*day) -= DAYS_PER_MONTH[(*month) - 1];
198  (*month)++;
199  }
200 
201  (*hour) = this->second / SECONDS_PER_HOUR;
202  (*minute) = (this->second - *hour * SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
203  (*second) = this->second % SECONDS_PER_MINUTE;
204 }
205 
209 int DateTime::getYear (void) const
210 {
211  int year;
212  short month;
213  short day;
214  short hour;
215  short minute;
216  short second;
217 
218  getDateParts(&year, &month, &day, &hour, &minute, &second);
219 
220  return year;
221 }
222 
226 int DateTime::getMonth (void) const
227 {
228  int year;
229  short month;
230  short day;
231  short hour;
232  short minute;
233  short second;
234 
235  getDateParts(&year, &month, &day, &hour, &minute, &second);
236 
237  return month + 1;
238 }
239 
243 int DateTime::getDay (void) const
244 {
245  int year;
246  short month;
247  short day;
248  short hour;
249  short minute;
250  short second;
251 
252  getDateParts(&year, &month, &day, &hour, &minute, &second);
253 
254  return day + 1;
255 }
256 
260 int DateTime::getHour (void) const
261 {
262  int year;
263  short month;
264  short day;
265  short hour;
266  short minute;
267  short second;
268 
269  getDateParts(&year, &month, &day, &hour, &minute, &second);
270 
271  return hour;
272 }
273 
277 int DateTime::getMinute (void) const
278 {
279  int year;
280  short month;
281  short day;
282  short hour;
283  short minute;
284  short second;
285 
286  getDateParts(&year, &month, &day, &hour, &minute, &second);
287 
288  return minute;
289 }
290 
294 int DateTime::getSecond (void) const
295 {
296  int year;
297  short month;
298  short day;
299  short hour;
300  short minute;
301  short second;
302 
303  getDateParts(&year, &month, &day, &hour, &minute, &second);
304 
305  return second;
306 }
307 
308 
322 const char* DateTime::asString (const char* format = "%Y-%m-%d %H:%M:%S") const
323 {
324  int year;
325  short month;
326  short day;
327  short hour;
328  short minute;
329  short second;
330 
331  getDateParts(&year, &month, &day, &hour, &minute, &second);
332 
333  /* calculate memory need */
334  int formatLength = strlen(format);
335  int dateLength = 0;
336  for (int i = 0; i < formatLength; i++) {
337  /* regular characters */
338  if (format[i] != '%') {
339  dateLength++;
340  continue;
341  }
342 
343  /* macros */
344  i++;
345  /* check for invalid macros */
346  if (i >= formatLength) {
347  dateLength = -1;
348  break;
349  }
350 
351  switch (format[i]) {
352  case '%':
353  dateLength++;
354  break;
355  case 'Y':
356  dateLength += log10(year) + 1;
357  break;
358  case 'm':
359  dateLength += 2;
360  break;
361  case 'd':
362  dateLength += 2;
363  break;
364  case 'H':
365  dateLength += 2;
366  break;
367  case 'M':
368  dateLength += 2;
369  break;
370  case 'S':
371  dateLength += 2;
372  break;
373  default:
374  dateLength = -1;
375  }
376 
377  if (dateLength == -1)
378  break;
379  }
380 
381  static char string[64] = "";
382  int stringIdx = 0;
383  for (int i = 0; i < formatLength; i++) {
384  if (format[i] != '%') {
385  string[stringIdx++] = format[i];
386  continue;
387  }
388 
389  i++;
390  switch (format[i]) {
391  case '%':
392  string[stringIdx++] = '%';
393  break;
394  case 'Y':
395  Com_sprintf(string + stringIdx, dateLength - stringIdx, "%d", year);
396  stringIdx += log10(year) + 1;
397  break;
398  case 'm':
399  Com_sprintf(string + stringIdx, dateLength - stringIdx, "%02d", month);
400  stringIdx += 2;
401  break;
402  case 'd':
403  Com_sprintf(string + stringIdx, dateLength - stringIdx, "%02d", day);
404  stringIdx += 2;
405  break;
406  case 'H':
407  Com_sprintf(string + stringIdx, dateLength - stringIdx, "%02d", hour);
408  stringIdx += 2;
409  break;
410  case 'M':
411  Com_sprintf(string + stringIdx, dateLength - stringIdx, "%02d", minute);
412  stringIdx += 2;
413  break;
414  case 'S':
415  Com_sprintf(string + stringIdx, dateLength - stringIdx, "%02d", second);
416  stringIdx += 2;
417  break;
418  default:
419  break;
420  }
421 
422  }
423  string[stringIdx++] = '\0';
424 
425  return string;
426 }
427 #endif
Class describing a point of time.
Definition: DateTime.h:30
static const short MONTHS_PER_YEAR
Definition: DateTime.h:39
DateTime & operator-=(const DateTime &other)
Substracts a DateTime (duration) from the left DateTime object.
Definition: DateTime.cpp:89
DateTime class definition.
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
DateTime operator-(const DateTime &other) const
Calcluate the difference of two DateTime objects in a new one.
Definition: DateTime.cpp:102
bool operator==(const DateTime &other) const
Decides whether two DateTimes are equal.
Definition: DateTime.cpp:110
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
DateTime(int day=0, int second=0)
Construct for DateTime class from date as days and time as seconds.
Definition: DateTime.cpp:34
static const int SECONDS_PER_DAY
Definition: DateTime.h:43
bool operator>(const DateTime &other) const
Decides if the left DateTime is later than the right one.
Definition: DateTime.cpp:142
static const int DAYS_PER_YEAR
Definition: DateTime.h:37
bool operator<=(const DateTime &other) const
Decides if the left DateTime is earlier or equal to the right one.
Definition: DateTime.cpp:134
int getTimeAsSeconds() const
Return the time part of the DateTime as seconds.
Definition: DateTime.cpp:54
DateTime & operator+=(const DateTime &other)
Adds a DateTime (duration) to the left DateTime object.
Definition: DateTime.cpp:66
void reCalculate(void)
Handle over- and underflowing date/time parts.
Definition: DateTime.cpp:160
int getDateAsDays() const
Return the date part of the DateTime as days.
Definition: DateTime.cpp:46
bool operator>=(const DateTime &other) const
Decides if the left DateTime is later or equal to the right one.
Definition: DateTime.cpp:150
QGL_EXTERN GLint i
Definition: r_gl.h:113
int second
Definition: DateTime.h:68
DateTime operator+(const DateTime &other) const
Calcluate the summary of two DateTime objects in a new one.
Definition: DateTime.cpp:79
static const short SECONDS_PER_MINUTE
Definition: DateTime.h:34
const int DAYS_PER_MONTH[DateTime::MONTHS_PER_YEAR]
Definition: cp_time.cpp:37
static const short SECONDS_PER_HOUR
Definition: DateTime.h:42
int day
Definition: DateTime.h:67
bool operator<(const DateTime &other) const
Decides if the left DateTime is earlier than the right one.
Definition: DateTime.cpp:126
void format(__printf__, 1, 2)))
bool operator!=(const DateTime &other) const
Decides whether two DateTimes are different.
Definition: DateTime.cpp:118