The Battle for Wesnoth  1.17.0-dev
irdya_datetime.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
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 irdya_date irdya_date::read_date(const std::string& date)
24 {
25  irdya_date date_result;
26 
27  // Currently only supports a year and an epoch.
28  std::size_t year_start = date.find_first_not_of(' ');
29  if(year_start == std::string::npos) {
30  // throw std::invalid_argument("Irdya date is missing year");
31  date_result.year = 0;
32  return date_result;
33  }
34 
35  std::size_t year_end = date.find_first_of(' ', year_start);
36  if(year_end == std::string::npos) {
37  year_end = date.size();
38  }
39 
40  date_result.year = std::stoi(date.substr(year_start, year_end - year_start));
41 
42  std::size_t epoch_start = date.find_first_not_of(' ', year_end);
43  if(epoch_start == std::string::npos) {
44  date_result.epoch = EPOCH::WESNOTH;
45  } else {
46  std::size_t epoch_end = date.find_first_of(' ', epoch_start);
47  date_result.epoch = EPOCH::string_to_enum(date.substr(epoch_start, epoch_end - epoch_start), EPOCH::WESNOTH);
48  }
49 
50  return date_result;
51 }
52 
53 std::string irdya_date::to_string() const
54 {
55  utils::string_map args {{"year", std::to_string(year)}};
56 
57  switch(epoch.v) {
58  case EPOCH::BEFORE_WESNOTH:
59  // TRANSLATORS: "Before Wesnoth" - format for years prior to the founding of Wesnoth
60  return VGETTEXT("$year BW", args);
61  case EPOCH::WESNOTH:
62  // TRANSLATORS: "Year of Wesnoth" - format for years after the founding of Wesnoth
63  return VGETTEXT("$year YW", args);
64  case EPOCH::BEFORE_FALL:
65  // TRANSLATORS: "Before the Fall" - format for years prior to the fall of Wesnoth
66  return VGETTEXT("$year BF", args);
67  case EPOCH::AFTER_FALL:
68  // TRANSLATORS: "After the Fall" - format for years after the fall of Wesnoth
69  return VGETTEXT("$year AF", args);
70  }
71 
72  return "";
73 }
74 
75 bool operator<(const irdya_date& a, const irdya_date& b)
76 {
77  if(!b.is_valid()) {
78  return a.is_valid();
79  }
80 
81  if(!a.is_valid()) {
82  return false;
83  }
84 
85  if(a.get_epoch().v < b.get_epoch().v) {
86  return true;
87  }
88 
89  if(a.get_epoch().v > b.get_epoch().v) {
90  return false;
91  }
92 
93  using EPOCH = irdya_date::EPOCH;
94 
95  // The BW and BF epochs count backward, much like BCE
96  if(a.get_epoch() == EPOCH::BEFORE_WESNOTH || a.get_epoch() == EPOCH::BEFORE_FALL) {
97  return (a.get_year() > b.get_year());
98  } else {
99  return (a.get_year() < b.get_year());
100  }
101 }
102 
103 bool operator>(const irdya_date& a, const irdya_date& b)
104 {
105  return b < a;
106 }
107 
108 bool operator<=(const irdya_date& a, const irdya_date& b)
109 {
110  return !(a > b);
111 }
112 
113 bool operator>=(const irdya_date& a, const irdya_date& b)
114 {
115  return !(a < b);
116 }
117 
118 bool operator==(const irdya_date& a, const irdya_date& b)
119 {
120  return a.get_year() == b.get_year() && a.get_epoch() == b.get_epoch();
121 }
122 
123 bool operator!=(const irdya_date& a, const irdya_date& b)
124 {
125  return !(a == b);
126 }
127 
128 std::ostream& operator<<(std::ostream& s, const irdya_date& d)
129 {
130  s << d.to_string();
131  return s;
132 }
bool is_valid() const
bool operator<(const irdya_date &a, const irdya_date &b)
std::map< std::string, t_string > string_map
#define a
#define d
bool operator>=(const irdya_date &a, const irdya_date &b)
std::string to_string() const
#define b
static irdya_date read_date(const std::string &date)
unsigned int year
static map_location::DIRECTION s
bool operator==(const irdya_date &a, const irdya_date &b)
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool operator!=(const irdya_date &a, const irdya_date &b)
unsigned int get_year() const
EPOCH get_epoch() const
bool operator<=(const irdya_date &a, const irdya_date &b)
std::ostream & operator<<(std::ostream &s, const irdya_date &d)
bool operator>(const irdya_date &a, const irdya_date &b)