The Battle for Wesnoth  1.17.0-dev
format_time_summary.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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"
20 #include "preferences/general.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
47  format_string = _("%H:%M");
48  }
49  else {
50  format_string = _("%I:%M %p");
51  }
52  } else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
53  // save is from this week
55  format_string = _("%A, %H:%M");
56  }
57  else {
58  format_string = _("%A, %I:%M %p");
59  }
60  } else {
61  // save is from current year
62  format_string = _("%b %d");
63  }
64  } else {
65  // save is from a different year
66  format_string = _("%b %d %Y");
67  }
68  assert(!format_string.empty());
69 
70  return translation::strftime(format_string, &save_time);
71 }
72 
73 }
static std::string _(const char *str)
Definition: gettext.hpp:93
std::string strftime(const std::string &format, const std::tm *time)
Definition: gettext.cpp:523
double t
Definition: astarsearch.cpp:65
std::string format_time_summary(std::time_t t)
bool use_twelve_hour_clock_format()
Definition: general.cpp:934