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 
22 #include <cassert>
23 #ifndef CPP20_CHRONO_SUPPORT
24 #include <ctime>
25 #endif
26 
27 namespace utils
28 {
29 std::string format_time_summary(const std::chrono::system_clock::time_point& t)
30 {
31 #ifdef CPP20_CHRONO_SUPPORT
32  const auto now = std::chrono::system_clock::now();
33  std::string format_string;
34 
35  auto curr_time = std::chrono::year_month_weekday{std::chrono::floor<std::chrono::days>(now)};
36  auto save_time = std::chrono::year_month_weekday{std::chrono::floor<std::chrono::days>(t)};
37 
38  if(curr_time == save_time) {
39  // save is from today
40  format_string = prefs::get().use_twelve_hour_clock_format()
41  // TRANSLATORS: 12-hour time, eg '1:59 PM'
42  // xgettext: no-c-format
43  ? _("%I:%M %p")
44  // TRANSLATORS: 24-hour time, eg '13:59'
45  // xgettext: no-c-format
46  : _("%H:%M");
47 
48  } else if(curr_time.month() == save_time.month() && curr_time.index() == save_time.index()) {
49  // save is from this week
50  format_string = prefs::get().use_twelve_hour_clock_format()
51  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
52  // xgettext: no-c-format
53  ? _("%A, %I:%M %p")
54  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
55  // xgettext: no-c-format
56  : _("%A, %H:%M");
57 
58  } else if(curr_time.year() == save_time.year()) {
59  // save is from current year
60  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
61  // xgettext: no-c-format
62  format_string = _("%b %d");
63 
64  } else {
65  // save is from a different year
66  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
67  // xgettext: no-c-format
68  format_string = _("%b %d %Y");
69  }
70 #else
71  const auto now = std::chrono::system_clock::now();
72 
73  auto as_time_t = std::chrono::system_clock::to_time_t(now);
74  const std::tm* timeptr = std::localtime(&as_time_t);
75  if(timeptr == nullptr) {
76  return "";
77  }
78 
79  const std::tm current_time = *timeptr;
80 
81  as_time_t = std::chrono::system_clock::to_time_t(t);
82  timeptr = std::localtime(&as_time_t);
83  if(timeptr == nullptr) {
84  return "";
85  }
86 
87  const std::tm save_time = *timeptr;
88 
89  std::string format_string;
90 
91  if(current_time.tm_year == save_time.tm_year) {
92  const int days_apart = current_time.tm_yday - save_time.tm_yday;
93  if(days_apart == 0) {
94  // save is from today
95  if(prefs::get().use_twelve_hour_clock_format() == false) {
96  // TRANSLATORS: 24-hour time, eg '13:59'
97  // xgettext: no-c-format
98  format_string = _("%H:%M");
99  }
100  else {
101  // TRANSLATORS: 12-hour time, eg '1:59 PM'
102  // xgettext: no-c-format
103  format_string = _("%I:%M %p");
104  }
105  } else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
106  // save is from this week
107  if(prefs::get().use_twelve_hour_clock_format() == false) {
108  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
109  // xgettext: no-c-format
110  format_string = _("%A, %H:%M");
111  }
112  else {
113  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
114  // xgettext: no-c-format
115  format_string = _("%A, %I:%M %p");
116  }
117  } else {
118  // save is from current year
119  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
120  // xgettext: no-c-format
121  format_string = _("%b %d");
122  }
123  } else {
124  // save is from a different year
125  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
126  // xgettext: no-c-format
127  format_string = _("%b %d %Y");
128  }
129 #endif
130  // TODO: make sure this doesn't result in #1709 coming back
131  assert(!format_string.empty());
133 }
134 
135 } // 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:62
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)