The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
format_time_summary.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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.hpp"
20 #include "strftime.hpp"
21 
22 namespace util {
23 
25  time_t curtime = time(nullptr);
26  const struct tm* timeptr = localtime(&curtime);
27  if(timeptr == nullptr) {
28  return "";
29  }
30 
31  const struct tm current_time = *timeptr;
32 
33  timeptr = localtime(&t);
34  if(timeptr == nullptr) {
35  return "";
36  }
37 
38  const struct 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  char buf[40];
71  const size_t res = util::strftime(buf, sizeof(buf), format_string, &save_time);
72  if(res == 0) {
73  buf[0] = 0;
74  }
75 
76  return buf;
77 }
78 
79 }
size_t strftime(char *str, size_t count, const std::string &format, const std::tm *time)
Definition: strftime.cpp:132
std::string format_time_summary(time_t t)
GLdouble GLdouble t
Definition: glew.h:1366
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
GLenum GLuint GLsizei const char * buf
Definition: glew.h:2498
GLuint res
Definition: glew.h:9258
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool use_twelve_hour_clock_format()