aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types/Date.cpp
blob: 73567681fb14e30685dccfd34855186df27b9ded (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "Date.hpp"

#include <algorithm>
#include <cassert>
#include <cctype>
#include <charconv>

#include "openvic-simulation/utility/Logger.hpp"
#include "openvic-simulation/utility/StringUtils.hpp"

using namespace OpenVic;

Timespan::Timespan(day_t value) : days { value } {}

bool Timespan::operator<(Timespan other) const {
   return days < other.days;
};
bool Timespan::operator>(Timespan other) const {
   return days > other.days;
};
bool Timespan::operator<=(Timespan other) const {
   return days <= other.days;
};
bool Timespan::operator>=(Timespan other) const {
   return days >= other.days;
};
bool Timespan::operator==(Timespan other) const {
   return days == other.days;
};
bool Timespan::operator!=(Timespan other) const {
   return days != other.days;
};

Timespan Timespan::operator+(Timespan other) const {
   return days + other.days;
}

Timespan Timespan::operator-(Timespan other) const {
   return days - other.days;
}

Timespan Timespan::operator*(day_t factor) const {
   return days * factor;
}

Timespan Timespan::operator/(day_t factor) const {
   return days / factor;
}

Timespan& Timespan::operator+=(Timespan other) {
   days += other.days;
   return *this;
}

Timespan& Timespan::operator-=(Timespan other) {
   days -= other.days;
   return *this;
}

Timespan& Timespan::operator++() {
   days++;
   return *this;
}

Timespan Timespan::operator++(int) {
   Timespan old = *this;
   ++(*this);
   return old;
}

Timespan::operator day_t() const {
   return days;
}

Timespan::operator double() const {
   return days;
}

std::string Timespan::to_string() const {
   return std::to_string(days);
}

Timespan::operator std::string() const {
   return to_string();
}

Timespan Timespan::from_years(day_t num) {
   return num * Date::DAYS_IN_YEAR;
}

Timespan Timespan::from_months(day_t num) {
   return (num / Date::MONTHS_IN_YEAR) * Date::DAYS_IN_YEAR + Date::DAYS_UP_TO_MONTH[num % Date::MONTHS_IN_YEAR];
}

Timespan Timespan::from_days(day_t num) {
   return num;
}

std::ostream& OpenVic::operator<<(std::ostream& out, Timespan const& timespan) {
   return out << timespan.to_string();
}

const std::string Date::MONTH_NAMES[MONTHS_IN_YEAR] = {
   "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November",
   "December"
};

Timespan Date::_date_to_timespan(year_t year, month_t month, day_t day) {
   month = std::clamp<month_t>(month, 1, MONTHS_IN_YEAR);
   day = std::clamp<day_t>(day, 1, DAYS_IN_MONTH[month - 1]);
   return year * DAYS_IN_YEAR + DAYS_UP_TO_MONTH[month - 1] + day - 1;
}

Timespan::day_t const* Date::DAYS_UP_TO_MONTH = generate_days_up_to_month();

Timespan::day_t const* Date::generate_days_up_to_month() {
   static Timespan::day_t days_up_to_month[MONTHS_IN_YEAR];
   Timespan::day_t days = 0;
   int month = 0;
   while (month < MONTHS_IN_YEAR) {
      days_up_to_month[month] = days;
      days += DAYS_IN_MONTH[month++];
   }
   assert(days == DAYS_IN_YEAR);
   return days_up_to_month;
}

Date::month_t const* Date::MONTH_FROM_DAY_IN_YEAR = generate_month_from_day_in_year();

Date::month_t const* Date::generate_month_from_day_in_year() {
   static month_t month_from_day_in_year[DAYS_IN_YEAR];
   Timespan::day_t days_left = 0;
   int day = 0, month = 0;
   while (day < DAYS_IN_YEAR) {
      days_left = (days_left > 0 ? days_left : DAYS_IN_MONTH[month++]) - 1;
      month_from_day_in_year[day++] = month;
   }
   assert(days_left == 0);
   assert(month_from_day_in_year[DAYS_IN_YEAR - 1] == MONTHS_IN_YEAR);
   return month_from_day_in_year;
}

Date::Date(Timespan total_days) : timespan { total_days } {
   if (timespan < 0) {
      Logger::error("Invalid timespan for date: ", timespan, " (cannot be negative)");
      timespan = 0;
   }
}

Date::Date(year_t year, month_t month, day_t day) : timespan { _date_to_timespan(year, month, day) } {}

Date::year_t Date::get_year() const {
   return static_cast<Timespan::day_t>(timespan) / DAYS_IN_YEAR;
}

Date::month_t Date::get_month() const {
   return MONTH_FROM_DAY_IN_YEAR[static_cast<Timespan::day_t>(timespan) % DAYS_IN_YEAR];
}

Date::day_t Date::get_day() const {
   return (static_cast<Timespan::day_t>(timespan) % DAYS_IN_YEAR) - DAYS_UP_TO_MONTH[get_month() - 1] + 1;
}

bool Date::operator<(Date other) const {
   return timespan < other.timespan;
};
bool Date::operator>(Date other) const {
   return timespan > other.timespan;
};
bool Date::operator<=(Date other) const {
   return timespan <= other.timespan;
};
bool Date::operator>=(Date other) const {
   return timespan >= other.timespan;
};
bool Date::operator==(Date other) const {
   return timespan == other.timespan;
};
bool Date::operator!=(Date other) const {
   return timespan != other.timespan;
};

Date Date::operator+(Timespan other) const {
   return timespan + other;
}

Timespan Date::operator-(Date other) const {
   return timespan - other.timespan;
}

Date& Date::operator+=(Timespan other) {
   timespan += other;
   return *this;
}

Date& Date::operator-=(Timespan other) {
   timespan -= other;
   return *this;
}

Date& Date::operator++() {
   timespan++;
   return *this;
}

Date Date::operator++(int) {
   Date old = *this;
   ++(*this);
   return old;
}

bool Date::in_range(Date start, Date end) const {
   return start <= *this && *this <= end;
}

std::string const& Date::get_month_name() const {
   const month_t month = get_month();
   if (1 <= month && month <= MONTHS_IN_YEAR) {
      return MONTH_NAMES[month - 1];
   }
   static const std::string invalid_month_name = "Invalid Month";
   return invalid_month_name;
}

std::string Date::to_string() const {
   std::stringstream ss;
   ss << *this;
   return ss.str();
}

Date::operator std::string() const {
   return to_string();
}

std::ostream& OpenVic::operator<<(std::ostream& out, Date date) {
   return out << static_cast<int>(date.get_year()) << Date::SEPARATOR_CHARACTER << static_cast<int>(date.get_month())
      << Date::SEPARATOR_CHARACTER << static_cast<int>(date.get_day());
}

// Parsed from string of the form YYYY.MM.DD
Date Date::from_string(char const* const str, char const* const end, bool* successful, bool quiet) {
   if (successful != nullptr) {
      *successful = true;
   }

   year_t year = 0;
   month_t month = 1;
   day_t day = 1;

   if (str == nullptr || end <= str) {
      if (!quiet) {
         Logger::error(
            "Invalid string start/end pointers: ", static_cast<void const*>(str), " - ", static_cast<void const*>(end)
         );
      }
      if (successful != nullptr) {
         *successful = false;
      }
      return { year, month, day };
   }

   char const* year_end = str;
   while (std::isdigit(*year_end) && ++year_end < end) {}

   if (year_end <= str) {
      if (!quiet) {
         Logger::error("Failed to find year digits in date: ", std::string_view { str, static_cast<size_t>(end - str) });
      }
      if (successful != nullptr) {
         *successful = false;
      }
      return { year, month, day };
   }

   bool sub_successful = false;
   uint64_t val = StringUtils::string_to_uint64(str, year_end, &sub_successful, 10);
   if (!sub_successful || val > std::numeric_limits<year_t>::max()) {
      if (!quiet) {
         Logger::error("Failed to read year: ", std::string_view { str, static_cast<size_t>(end - str) });
      }
      if (successful != nullptr) {
         *successful = false;
      }
      return { year, month, day };
   }
   year = val;
   if (year_end < end) {
      if (*year_end == SEPARATOR_CHARACTER) {
         char const* const month_start = year_end + 1;
         char const* month_end = month_start;
         if (month_start < end) {
            while (std::isdigit(*month_end) && ++month_end < end) {}
         }
         if (month_start >= month_end) {
            if (!quiet) {
               Logger::error(
                  "Failed to find month digits in date: ", std::string_view { str, static_cast<size_t>(end - str) }
               );
            }
            if (successful != nullptr) {
               *successful = false;
            }
         } else {
            sub_successful = false;
            val = StringUtils::string_to_uint64(month_start, month_end, &sub_successful, 10);
            if (!sub_successful || val < 1 || val > MONTHS_IN_YEAR) {
               if (!quiet) {
                  Logger::error("Failed to read month: ", std::string_view { str, static_cast<size_t>(end - str) });
               }
               if (successful != nullptr) {
                  *successful = false;
               }
            } else {
               month = val;
               if (month_end < end) {
                  if (*month_end == SEPARATOR_CHARACTER) {
                     char const* const day_start = month_end + 1;
                     char const* day_end = day_start;
                     if (day_start < end) {
                        while (std::isdigit(*day_end) && ++day_end < end) {}
                     }
                     if (day_start >= day_end) {
                        if (!quiet) {
                           Logger::error(
                              "Failed to find day digits in date: ",
                              std::string_view { str, static_cast<size_t>(end - str) }
                           );
                        }
                        if (successful != nullptr) {
                           *successful = false;
                        }
                     } else {
                        sub_successful = false;
                        val = StringUtils::string_to_uint64(day_start, day_end, &sub_successful);
                        if (!sub_successful || val < 1 || val > DAYS_IN_MONTH[month - 1]) {
                           if (!quiet) {
                              Logger::error(
                                 "Failed to read day: ", std::string_view { str, static_cast<size_t>(end - str) }
                              );
                           }
                           if (successful != nullptr) {
                              *successful = false;
                           }
                        } else {
                           day = val;
                           if (day_end < end) {
                              if (!quiet) {
                                 Logger::error(
                                    "Unexpected string \"",
                                    std::string_view { day_end, static_cast<size_t>(end - day_end) },
                                    "\" at the end of date ",
                                    std::string_view { str, static_cast<size_t>(end - str) }
                                 );
                              }
                              if (successful != nullptr) {
                                 *successful = false;
                              }
                           }
                        }
                     }
                  } else {
                     if (!quiet) {
                        Logger::error(
                           "Unexpected character \"", *month_end, "\" in month of date ",
                           std::string_view { str, static_cast<size_t>(end - str) }
                        );
                     }
                     if (successful != nullptr) {
                        *successful = false;
                     }
                  }
               }
            }
         }
      } else {
         if (!quiet) {
            Logger::error(
               "Unexpected character \"", *year_end, "\" in year of date ",
               std::string_view { str, static_cast<size_t>(end - str) }
            );
         }
         if (successful != nullptr) {
            *successful = false;
         }
      }
   }
   return { year, month, day };
};

Date Date::from_string(char const* str, size_t length, bool* successful, bool quiet) {
   return from_string(str, str + length, successful, quiet);
}

Date Date::from_string(std::string_view str, bool* successful, bool quiet) {
   return from_string(str.data(), str.length(), successful, quiet);
}