The Battle for Wesnoth  1.19.7+dev
irdya_datetime.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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 "tstring.hpp"
19 
20 
21 using namespace utils;
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 = wesnoth_epoch::type::wesnoth;
45  } else {
46  std::size_t epoch_end = date.find_first_of(' ', epoch_start);
47  date_result.epoch = wesnoth_epoch::get_enum(date.substr(epoch_start, epoch_end - epoch_start)).value_or(wesnoth_epoch::type::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) {
58  case wesnoth_epoch::type::before_wesnoth:
59  // TRANSLATORS: "Before Wesnoth" - format for years prior to the founding of Wesnoth
60  return VGETTEXT("$year BW", args);
61  case wesnoth_epoch::type::wesnoth:
62  // TRANSLATORS: "Year of Wesnoth" - format for years after the founding of Wesnoth
63  return VGETTEXT("$year YW", args);
64  case wesnoth_epoch::type::before_fall:
65  // TRANSLATORS: "Before the Fall" - format for years prior to the fall of Wesnoth
66  return VGETTEXT("$year BF", args);
67  case wesnoth_epoch::type::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 utils::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() < b.get_epoch()) {
86  return true;
87  }
88 
89  if(a.get_epoch() > b.get_epoch()) {
90  return false;
91  }
92 
93  // The BW and BF epochs count backward, much like BCE
94  if(a.get_epoch() == wesnoth_epoch::type::before_wesnoth || a.get_epoch() == wesnoth_epoch::type::before_fall) {
95  return (a.get_year() > b.get_year());
96  } else {
97  return (a.get_year() < b.get_year());
98  }
99 }
100 
101 bool utils::operator>(const irdya_date& a, const irdya_date& b)
102 {
103  return b < a;
104 }
105 
106 bool utils::operator<=(const irdya_date& a, const irdya_date& b)
107 {
108  return !(a > b);
109 }
110 
111 bool utils::operator>=(const irdya_date& a, const irdya_date& b)
112 {
113  return !(a < b);
114 }
115 
116 bool utils::operator==(const irdya_date& a, const irdya_date& b)
117 {
118  return a.get_year() == b.get_year() && a.get_epoch() == b.get_epoch();
119 }
120 
121 bool utils::operator!=(const irdya_date& a, const irdya_date& b)
122 {
123  return !(a == b);
124 }
125 
126 std::ostream& utils::operator<<(std::ostream& s, const irdya_date& d)
127 {
128  s << d.to_string();
129  return s;
130 }
Calendar for handling and comparing dates using the common epoches of the storyline.
unsigned int get_year() const
static irdya_date read_date(const std::string &date)
wesnoth_epoch::type epoch
bool is_valid() const
std::string to_string() const
wesnoth_epoch::type get_epoch() const
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool operator!=(const irdya_date &a, const irdya_date &b)
bool operator<=(const irdya_date &a, const irdya_date &b)
bool operator>(const irdya_date &a, const irdya_date &b)
bool operator==(const irdya_date &a, const irdya_date &b)
int stoi(std::string_view str)
Same interface as std::stoi and meant as a drop in replacement, except:
Definition: charconv.hpp:154
std::map< std::string, t_string > string_map
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)
static constexpr utils::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 d
#define b