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