16 #include <boost/test/unit_test.hpp>
40 inline std::string minifmt(time_t
t,
const std::string& singular,
const std::string& plural)
42 return t ? std::to_string(
t) +
" " + (
t > 1 ? plural : singular) :
"";
52 time_t > time_detailed;
54 inline time_t gen_as_time_t(
const time_detailed& params)
56 auto [sec, min, hr, day, wk, mo, yr] = params;
58 return YEAR*yr + MONTH*mo + WEEK*wk + DAY*day + HOUR*hr + MIN*min + SEC*sec;
61 inline std::string gen_as_str(
const time_detailed& params)
63 auto [sec, min, hr, day, wk, mo, yr] = params;
65 std::vector<t_string> bits;
68 bits.emplace_back(minifmt(yr,
"year",
"years"));
69 bits.emplace_back(minifmt(mo,
"month",
"months"));
70 bits.emplace_back(minifmt(wk,
"week",
"weeks"));
71 bits.emplace_back(minifmt(day,
"day",
"days"));
72 bits.emplace_back(minifmt(hr,
"hour",
"hours"));
73 bits.emplace_back(minifmt(min,
"minute",
"minutes"));
74 bits.emplace_back(minifmt(sec,
"second",
"seconds"));
77 auto p = std::remove_if(bits.begin(), bits.end(), [](
const t_string&
t) { return t.empty(); });
85 inline void test_format_timespan(
const time_detailed& tcase,
const std::string& detailed,
const std::string& fuzzy=
"")
90 BOOST_REQUIRE_NE(detailed, fuzzy);
100 test_format_timespan({ 1, 0, 0, 0, 0, 0, 0 },
"1 second");
102 test_format_timespan({ 2, 0, 0, 0, 0, 0, 0 },
"2 seconds");
104 test_format_timespan({ 0, 1, 0, 0, 0, 0, 0 },
"1 minute");
106 test_format_timespan({ 0, 2, 0, 0, 0, 0, 0 },
"2 minutes");
108 test_format_timespan({ 0, 0, 1, 0, 0, 0, 0 },
"1 hour");
110 test_format_timespan({ 0, 0, 2, 0, 0, 0, 0 },
"2 hours");
112 test_format_timespan({ 0, 0, 0, 1, 0, 0, 0 },
"1 day");
114 test_format_timespan({ 0, 0, 0, 2, 0, 0, 0 },
"2 days");
116 test_format_timespan({ 0, 0, 0, 0, 1, 0, 0 },
"1 week");
118 test_format_timespan({ 0, 0, 0, 0, 2, 0, 0 },
"2 weeks");
120 test_format_timespan({ 0, 0, 0, 0, 0, 1, 0 },
"1 month");
122 test_format_timespan({ 0, 0, 0, 0, 0, 2, 0 },
"2 months");
124 test_format_timespan({ 0, 0, 0, 0, 0, 0, 1 },
"1 year");
126 test_format_timespan({ 0, 0, 0, 0, 0, 0, 2 },
"2 years");
128 auto t = time_detailed{ 12, 1, 23, 3, 2, 5, 2 };
129 test_format_timespan(
t, gen_as_str(
t),
"2 years");
131 t = time_detailed{ 0, 0, 0, 0, 0, 0, 0 };
137 test_format_timespan({ 4, 0, 49, 0, 0, 0, 0 },
"2 days, 1 hour, and 4 seconds",
"2 days");
139 test_format_timespan({ 0, 40, 0, 11, 1, 0, 4 },
"4 years, 2 weeks, 4 days, and 40 minutes",
"4 years");
141 test_format_timespan({ 0, 0, 1, 0, 0, 3, 4 },
"4 years, 3 months, and 1 hour",
"4 years");
143 test_format_timespan({ 10, 0, 0, 0, 0, 2, 0 },
"2 months and 10 seconds",
"2 months");
146 BOOST_AUTO_TEST_SUITE_END()
std::string format_timespan(std::time_t time, bool detailed)
Formats a timespan into human-readable text for player authentication functions.
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
BOOST_AUTO_TEST_SUITE(filesystem)