The Battle for Wesnoth  1.19.27+dev
format_time_summary.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "format_time_summary.hpp"
17 
18 #include "gettext.hpp"
20 #include "serialization/chrono.hpp"
21 #include "utils/general.hpp"
22 
23 #include <cassert>
24 #ifndef CPP20_CHRONO_SUPPORT
25 #include <ctime>
26 #endif
27 
28 namespace utils
29 {
30 std::string format_time_summary(const std::chrono::system_clock::time_point& t)
31 {
32 #ifdef CPP20_CHRONO_SUPPORT
33  const auto now = std::chrono::system_clock::now();
34  std::string format_string;
35 
36  auto curr_time = std::chrono::year_month_weekday{std::chrono::floor<std::chrono::days>(now)};
37  auto save_time = std::chrono::year_month_weekday{std::chrono::floor<std::chrono::days>(t)};
38 
39  if(curr_time == save_time) {
40  // save is from today
41  format_string = prefs::get().use_twelve_hour_clock_format()
42  // TRANSLATORS: 12-hour time, eg '1:59 PM'
43  // xgettext: no-c-format
44  ? _("%I:%M %p")
45  // TRANSLATORS: 24-hour time, eg '13:59'
46  // xgettext: no-c-format
47  : _("%H:%M");
48 
49  } else if(curr_time.month() == save_time.month() && curr_time.index() == save_time.index()) {
50  // save is from this week
51  format_string = prefs::get().use_twelve_hour_clock_format()
52  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
53  // xgettext: no-c-format
54  ? _("%A, %I:%M %p")
55  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
56  // xgettext: no-c-format
57  : _("%A, %H:%M");
58 
59  } else if(curr_time.year() == save_time.year()) {
60  // save is from current year
61  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
62  // xgettext: no-c-format
63  format_string = _("%b %d");
64 
65  } else {
66  // save is from a different year
67  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
68  // xgettext: no-c-format
69  format_string = _("%b %d %Y");
70  }
71 #else
72  const auto now = std::chrono::system_clock::now();
73 
74  auto as_time_t = std::chrono::system_clock::to_time_t(now);
75  std::tm current_time{};
76  if(utils::localtime_r(&as_time_t, &current_time) == nullptr) {
77  return "";
78  }
79 
80  as_time_t = std::chrono::system_clock::to_time_t(t);
81  std::tm save_time{};
82  if(utils::localtime_r(&as_time_t, &save_time) == nullptr) {
83  return "";
84  }
85 
86  std::string format_string;
87 
88  if(current_time.tm_year == save_time.tm_year) {
89  const int days_apart = current_time.tm_yday - save_time.tm_yday;
90  if(days_apart == 0) {
91  // save is from today
92  if(prefs::get().use_twelve_hour_clock_format() == false) {
93  // TRANSLATORS: 24-hour time, eg '13:59'
94  // xgettext: no-c-format
95  format_string = _("%H:%M");
96  }
97  else {
98  // TRANSLATORS: 12-hour time, eg '1:59 PM'
99  // xgettext: no-c-format
100  format_string = _("%I:%M %p");
101  }
102  } else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
103  // save is from this week
104  if(prefs::get().use_twelve_hour_clock_format() == false) {
105  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
106  // xgettext: no-c-format
107  format_string = _("%A, %H:%M");
108  }
109  else {
110  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
111  // xgettext: no-c-format
112  format_string = _("%A, %I:%M %p");
113  }
114  } else {
115  // save is from current year
116  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
117  // xgettext: no-c-format
118  format_string = _("%b %d");
119  }
120  } else {
121  // save is from a different year
122  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
123  // xgettext: no-c-format
124  format_string = _("%b %d %Y");
125  }
126 #endif
127  // TODO: make sure this doesn't result in #1709 coming back
128  assert(!format_string.empty());
130 }
131 
132 } // namespace utils
double t
Definition: astarsearch.cpp:63
static prefs & get()
bool use_twelve_hour_clock_format()
static std::string _(const char *str)
Definition: gettext.hpp:100
auto get_local_timestamp(const std::chrono::system_clock::time_point &time)
Definition: chrono.hpp:63
std::string translate_timestamp(time_t time, std::string_view format)
Translates the passed timestamp and returns it in the requested format.
Definition: gettext.cpp:577
std::string format_time_summary(const std::chrono::system_clock::time_point &t)
std::tm * localtime_r(const std::time_t *time, std::tm *result)
Definition: general.hpp:175