The Battle for Wesnoth  1.19.10+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  ? _("%I:%M %p")
43  // TRANSLATORS: 24-hour time, eg '13:59'
44  : _("%H:%M");
45 
46  } else if(curr_time.month() == save_time.month() && curr_time.index() == save_time.index()) {
47  // save is from this week
48  format_string = prefs::get().use_twelve_hour_clock_format()
49  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
50  ? _("%A, %I:%M %p")
51  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
52  : _("%A, %H:%M");
53 
54  } else if(curr_time.year() == save_time.year()) {
55  // save is from current year
56  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
57  format_string = _("%b %d");
58 
59  } else {
60  // save is from a different year
61  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
62  format_string = _("%b %d %Y");
63  }
64 #else
65  const auto now = std::chrono::system_clock::now();
66 
67  auto as_time_t = std::chrono::system_clock::to_time_t(now);
68  const std::tm* timeptr = std::localtime(&as_time_t);
69  if(timeptr == nullptr) {
70  return "";
71  }
72 
73  const std::tm current_time = *timeptr;
74 
75  as_time_t = std::chrono::system_clock::to_time_t(t);
76  timeptr = std::localtime(&as_time_t);
77  if(timeptr == nullptr) {
78  return "";
79  }
80 
81  const std::tm save_time = *timeptr;
82 
83  std::string format_string;
84 
85  if(current_time.tm_year == save_time.tm_year) {
86  const int days_apart = current_time.tm_yday - save_time.tm_yday;
87  if(days_apart == 0) {
88  // save is from today
89  if(prefs::get().use_twelve_hour_clock_format() == false) {
90  // TRANSLATORS: 24-hour time, eg '13:59'
91  format_string = _("%H:%M");
92  }
93  else {
94  // TRANSLATORS: 12-hour time, eg '1:59 PM'
95  format_string = _("%I:%M %p");
96  }
97  } else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
98  // save is from this week
99  if(prefs::get().use_twelve_hour_clock_format() == false) {
100  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
101  format_string = _("%A, %H:%M");
102  }
103  else {
104  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
105  format_string = _("%A, %I:%M %p");
106  }
107  } else {
108  // save is from current year
109  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
110  format_string = _("%b %d");
111  }
112  } else {
113  // save is from a different year
114  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
115  format_string = _("%b %d %Y");
116  }
117 #endif
118  // TODO: make sure this doesn't result in #1709 coming back
119  assert(!format_string.empty());
120  return chrono::format_local_timestamp(t, format_string);
121 }
122 
123 } // 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:97
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition: chrono.hpp:62
std::string format_time_summary(const std::chrono::system_clock::time_point &t)