The Battle for Wesnoth  1.19.5+dev
format_time_summary.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 "assert.h"
19 #include "gettext.hpp"
21 
22 namespace utils {
23 
24 std::string format_time_summary(std::time_t t) {
25  std::time_t curtime = std::time(nullptr);
26  const std::tm* timeptr = std::localtime(&curtime);
27  if(timeptr == nullptr) {
28  return "";
29  }
30 
31  const std::tm current_time = *timeptr;
32 
33  timeptr = std::localtime(&t);
34  if(timeptr == nullptr) {
35  return "";
36  }
37 
38  const std::tm save_time = *timeptr;
39 
40  std::string format_string;
41 
42  if(current_time.tm_year == save_time.tm_year) {
43  const int days_apart = current_time.tm_yday - save_time.tm_yday;
44  if(days_apart == 0) {
45  // save is from today
46  if(prefs::get().use_twelve_hour_clock_format() == false) {
47  // TRANSLATORS: 24-hour time, eg '13:59'
48  format_string = _("%H:%M");
49  }
50  else {
51  // TRANSLATORS: 12-hour time, eg '1:59 PM'
52  format_string = _("%I:%M %p");
53  }
54  } else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
55  // save is from this week
56  if(prefs::get().use_twelve_hour_clock_format() == false) {
57  // TRANSLATORS: Day of week + 24-hour time, eg 'Sunday, 13:59'
58  format_string = _("%A, %H:%M");
59  }
60  else {
61  // TRANSLATORS: Day of week + 12-hour time, eg 'Sunday, 1:59 PM'
62  format_string = _("%A, %I:%M %p");
63  }
64  } else {
65  // save is from current year
66  // TRANSLATORS: Month + day of month, eg 'Nov 02'. Format for your locale.
67  format_string = _("%b %d");
68  }
69  } else {
70  // save is from a different year
71  // TRANSLATORS: Month + day of month + year, eg 'Nov 02 2021'. Format for your locale.
72  format_string = _("%b %d %Y");
73  }
74  assert(!format_string.empty());
75 
76  return translation::strftime(format_string, &save_time);
77 }
78 
79 }
double t
Definition: astarsearch.cpp:63
static prefs & get()
static std::string _(const char *str)
Definition: gettext.hpp:93
std::string strftime(const std::string &format, const std::tm *time)
Definition: gettext.cpp:555
std::string format_time_summary(std::time_t t)