The Battle for Wesnoth  1.15.1+dev
format_time_summary.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "format_time_summary.hpp"
16 
17 #include "assert.h"
18 #include "gettext.hpp"
19 #include "preferences/general.hpp"
20 
21 namespace utils {
22 
23 std::string format_time_summary(std::time_t t) {
24  std::time_t curtime = std::time(nullptr);
25  const std::tm* timeptr = std::localtime(&curtime);
26  if(timeptr == nullptr) {
27  return "";
28  }
29 
30  const std::tm current_time = *timeptr;
31 
32  timeptr = std::localtime(&t);
33  if(timeptr == nullptr) {
34  return "";
35  }
36 
37  const std::tm save_time = *timeptr;
38 
39  std::string format_string;
40 
41  if(current_time.tm_year == save_time.tm_year) {
42  const int days_apart = current_time.tm_yday - save_time.tm_yday;
43  if(days_apart == 0) {
44  // save is from today
46  format_string = _("%H:%M");
47  }
48  else {
49  format_string = _("%I:%M %p");
50  }
51  } else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
52  // save is from this week
54  format_string = _("%A, %H:%M");
55  }
56  else {
57  format_string = _("%A, %I:%M %p");
58  }
59  } else {
60  // save is from current year
61  format_string = _("%b %d");
62  }
63  } else {
64  // save is from a different year
65  format_string = _("%b %d %Y");
66  }
67  assert(!format_string.empty());
68 
69  return translation::strftime(format_string, &save_time);
70 }
71 
72 }
std::string strftime(const std::string &format, const std::tm *time)
Definition: gettext.cpp:508
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
double t
Definition: astarsearch.cpp:64
std::string format_time_summary(std::time_t t)
bool use_twelve_hour_clock_format()
Definition: general.cpp:1042