Repository navigation
Expand file tree
/
Copy pathtime.cpp
More file actions
160 lines (138 loc) · 5.36 KB
/
Copy pathtime.cpp
File metadata and controls
160 lines (138 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "utils/time.hpp"
#include "graphics/irr_driver.hpp"
#include "utils/log.hpp"
#include "utils/translation.hpp"
#include <ctime>
#include <IrrlichtDevice.h>
irr::ITimer *StkTime::m_timer = NULL;
std::chrono::steady_clock::time_point
StkTime::m_mono_start = std::chrono::steady_clock::now();
/** Init function for the timer. It grabs a copy of the timer of the
* current irrlicht device (which is the NULL device). This way the
* irrlicht time routine can be used even if no device exists. This
* situation can happen when the window resolution is changed - if the
* sfx manager (in a separate thread) would access the timer while the
* device does not exist, stk crashes.
*/
void StkTime::init()
{
assert(!m_timer);
m_timer = irr_driver->getDevice()->getTimer();
m_timer->grab();
} // init
// ----------------------------------------------------------------------------
/** Get the time in string for game server logging prefix (thread-safe)*/
std::string StkTime::getLogTime()
{
time_t time_now = 0;
time(&time_now);
std::tm timeptr = {};
#ifdef WIN32
localtime_s(&timeptr, &time_now);
#else
localtime_r(&time_now, &timeptr);
#endif
std::string result;
result.resize(64);
strftime(&result[0], 64, "%a %b %d %H:%M:%S %Y", &timeptr);
size_t len = strlen(result.c_str());
result.resize(len);
return result;
} // getLogTime
// ----------------------------------------------------------------------------
/** Converts the time in this object to a human readable string. */
std::string StkTime::toString(const TimeType &tt)
{
const struct tm *t = gmtime(&tt);
std::string date_format = getDateFormat();
char s[64];
strftime(s, 64, date_format.c_str(), t);
return s;
} // toString(1)
// ----------------------------------------------------------------------------
/** Converts the date represented by year, month, day
* to a human readable string. */
std::string StkTime::toString(int year, int month, int day)
{
struct tm *t = new tm();
t->tm_year = year - 1900;
t->tm_mon = month - 1;
t->tm_mday = day;
std::string date_format = getDateFormat();
char s[64];
strftime(s, 64, date_format.c_str(), t);
delete t;
return s;
} // toString(3)
// ----------------------------------------------------------------------------
/** Obtains the translated format of the time string. */
std::string StkTime::getDateFormat()
{
//I18N: Format for dates (%d = day, %m = month, %Y = year). See http://www.cplusplus.com/reference/ctime/strftime/ for more info about date formats.
core::stringw w_date_format = translations->w_gettext(N_("%d/%m/%Y"));
core::stringc c_date_format(w_date_format.c_str());
std::string date_format(c_date_format.c_str());
if (date_format.find("%d", 0) == std::string::npos || // substring not found
date_format.find("%m", 0) == std::string::npos ||
date_format.find("%Y", 0) == std::string::npos)
{
Log::warn("Time", "Incorrect date format in translation, using default format.");
date_format = "%d/%m/%Y";
}
return date_format;
} // toString
// ----------------------------------------------------------------------------
/** Returns a time based on an arbitrary 'epoch' (e.g. could be start
* time of the application, 1.1.1970, ...).
* The value is a double precision floating point value in seconds.
*/
double StkTime::getRealTime(long startAt)
{
assert(m_timer);
return m_timer->getRealTime()/1000.0;
} // getTimeSinceEpoch
// ----------------------------------------------------------------------------
/** Returns the current date.
* \param day Day (1 - 31).
* \param month (1-12).
* \param year (4 digits).
*/
void StkTime::getDate(int *day, int *month, int *year)
{
std::time_t t = std::time(0); // get time now
std::tm * now = std::localtime(&t);
if(day) *day = now->tm_mday;
if(month) *month = now->tm_mon + 1;
if(year) *year = now->tm_year + 1900;
} // getDate
// ----------------------------------------------------------------------------
StkTime::ScopeProfiler::ScopeProfiler(const char* name)
{
Log::info("ScopeProfiler", "%s {\n", name);
m_time = getMonoTimeMs();
m_name = name;
} // StkTime::ScopeProfiler::ScopeProfiler
// ----------------------------------------------------------------------------
StkTime::ScopeProfiler::~ScopeProfiler()
{
uint64_t difference = getMonoTimeMs() - m_time;
Log::info("ScopeProfiler", "} // took %d ms (%s)\n",
(int)difference, m_name.c_str());
} // StkTime::ScopeProfiler::ScopeProfiler