The Battle for Wesnoth  1.17.10+dev
irdya_datetime.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2022
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "utils/irdya_datetime.hpp"
16 
17 #include "formula/string_utils.hpp"
18 #include "gettext.hpp"
19 #include "tstring.hpp"
20 
21 #include <exception>
22 
23 using namespace utils;
24 
25 irdya_date irdya_date::read_date(const std::string& date)
26 {
27  irdya_date date_result;
28 
29  // Currently only supports a year and an epoch.
30  std::size_t year_start = date.find_first_not_of(' ');
31  if(year_start == std::string::npos) {
32  // throw std::invalid_argument("Irdya date is missing year");
33  date_result.year = 0;
34  return date_result;
35  }
36 
37  std::size_t year_end = date.find_first_of(' ', year_start);
38  if(year_end == std::string::npos) {
39  year_end = date.size();
40  }
41 
42  date_result.year = std::stoi(date.substr(year_start, year_end - year_start));
43 
44  std::size_t epoch_start = date.find_first_not_of(' ', year_end);
45  if(epoch_start == std::string::npos) {
46  date_result.epoch = wesnoth_epoch::type::wesnoth;
47  } else {
48  std::size_t epoch_end = date.find_first_of(' ', epoch_start);
49  date_result.epoch = wesnoth_epoch::get_enum(date.substr(epoch_start, epoch_end - epoch_start)).value_or(wesnoth_epoch::type::wesnoth);
50  }
51 
52  return date_result;
53 }
54 
55 std::string irdya_date::to_string() const
56 {
57  utils::string_map args {{"year", std::to_string(year)}};
58 
59  switch(epoch) {
60  case wesnoth_epoch::type::before_wesnoth:
61  // TRANSLATORS: "Before Wesnoth" - format for years prior to the founding of Wesnoth
62  return VGETTEXT("$year BW", args);
63  case wesnoth_epoch::type::wesnoth:
64  // TRANSLATORS: "Year of Wesnoth" - format for years after the founding of Wesnoth
65  return VGETTEXT("$year YW", args);
66  case wesnoth_epoch::type::before_fall:
67  // TRANSLATORS: "Before the Fall" - format for years prior to the fall of Wesnoth
68  return VGETTEXT("$year BF", args);
69  case wesnoth_epoch::type::after_fall:
70  // TRANSLATORS: "After the Fall" - format for years after the fall of Wesnoth
71  return VGETTEXT("$year AF", args);
72  }
73 
74  return "";
75 }
76 
78 {
79  if(!b.is_valid()) {
80  return a.is_valid();
81  }
82 
83  if(!a.is_valid()) {
84  return false;
85  }
86 
87  if(a.get_epoch() < b.get_epoch()) {
88  return true;
89  }
90 
91  if(a.get_epoch() > b.get_epoch()) {
92  return false;
93  }
94 
95  // The BW and BF epochs count backward, much like BCE
96  if(a.get_epoch() == wesnoth_epoch::type::before_wesnoth || a.get_epoch() == wesnoth_epoch::type::before_fall) {
97  return (a.get_year() > b.get_year());
98  } else {
99  return (a.get_year() < b.get_year());
100  }
101 }
102 
104 {
105  return b < a;
106 }
107 
109 {
110  return !(a > b);
111 }
112 
114 {
115  return !(a < b);
116 }
117 
119 {
120  return a.get_year() == b.get_year() && a.get_epoch() == b.get_epoch();
121 }
122 
124 {
125  return !(a == b);
126 }
127 
128 std::ostream& utils::operator<<(std::ostream& s, const irdya_date& d)
129 {
130  s << d.to_string();
131  return s;
132 }
std::string to_string() const
std::map< std::string, t_string > string_map
Calendar for handling and comparing dates using the common epoches of the storyline.
#define a
#define d
wesnoth_epoch::type get_epoch() const
#define b
bool operator<(const irdya_date &a, const irdya_date &b)
wesnoth_epoch::type epoch
static constexpr std::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
Definition: enum_base.hpp:57
static map_location::DIRECTION s
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool operator!=(const irdya_date &a, const irdya_date &b)
std::ostream & operator<<(std::ostream &s, const irdya_date &d)
unsigned int get_year() const
bool is_valid() const
bool operator<=(const irdya_date &a, const irdya_date &b)
bool operator>(const irdya_date &a, const irdya_date &b)
static irdya_date read_date(const std::string &date)
bool operator==(const irdya_date &a, const irdya_date &b)
bool operator>=(const irdya_date &a, const irdya_date &b)