SuperTuxKart
Loading...
Searching...
No Matches
time.hpp
1// SuperTuxKart - a fun racing game with go-kart
2//
3// Copyright (C) 2013-2015 SuperTuxKart-Team
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_TIME_HPP
20#define HEADER_TIME_HPP
21
22#include "ITimer.h"
23
24#include <chrono>
25#include <stdexcept>
26
27#include "utils/types.hpp"
28
29#ifdef WIN32
30# define WIN32_LEAN_AND_MEAN
31# include <windows.h>
32#else
33# include <stdint.h>
34# include <sys/time.h>
35# include <unistd.h>
36#endif
37
38#include <time.h>
39#include <string>
40#include <stdio.h>
41
43{
44private:
48 static irr::ITimer *m_timer;
49
51 static std::chrono::steady_clock::time_point m_mono_start;
52public:
53 typedef time_t TimeType;
54
55 static void init();
56 static void getDate(int *day=NULL, int *month=NULL, int *year=NULL);
57
58 // ------------------------------------------------------------------------
60 static std::string getLogTime();
61 // ------------------------------------------------------------------------
63 static std::string toString(const TimeType &tt);
64 // ------------------------------------------------------------------------
68 static TimeType getTimeSinceEpoch()
69 {
70#ifdef WIN32
71 FILETIME ft;
72 GetSystemTimeAsFileTime(&ft);
73 __int64 t = ft.dwHighDateTime;
74 t <<= 32;
75 t /= 10;
76 // The Unix epoch starts on Jan 1 1970. Need to subtract
77 // the difference in seconds from Jan 1 1601.
78# if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
79# define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
80# else
81# define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
82# endif
83 t -= DELTA_EPOCH_IN_MICROSECS;
84
85 t |= ft.dwLowDateTime;
86 // Convert to seconds since epoch
87 t /= 1000000UL;
88 return t;
89#else
90 struct timeval tv;
91 gettimeofday(&tv, NULL);
92 return tv.tv_sec;
93#endif
94 }; // getTimeSinceEpoch
95
96 // ------------------------------------------------------------------------
101 static double getRealTime(long startAt=0);
102 // ------------------------------------------------------------------------
106 static uint64_t getMonoTimeMs()
107 {
108 auto duration = std::chrono::steady_clock::now() - m_mono_start;
109 auto value =
110 std::chrono::duration_cast<std::chrono::milliseconds>(duration);
111 return value.count();
112 }
113 // ------------------------------------------------------------------------
118 static int compareTime(TimeType time1, TimeType time2)
119 {
120 double diff = difftime(time1, time2);
121
122 if (diff > 0)
123 return 1;
124 else if (diff < 0)
125 return -1;
126 else
127 return 0;
128 }; // compareTime
129
130 // ------------------------------------------------------------------------
134 static void sleep(int msec)
135 {
136#ifdef WIN32
137 Sleep(msec);
138#else
139 usleep(msec*1000);
140#endif
141 } // sleep
142 // ------------------------------------------------------------------------
146 static TimeType addInterval(TimeType time, int year, int month, int day) {
147 struct tm t = *gmtime(&time);
148 t.tm_year += year;
149 t.tm_mon += month;
150 t.tm_mday += day;
151 return mktime(&t);
152 } // addInterval
153
154 // ------------------------------------------------------------------------
156 {
157 uint64_t m_time;
158 std::string m_name;
159 public:
160 ScopeProfiler(const char* name);
162 }; // class ScopeProfiler
163
164}; // namespace time
165#endif
166
Definition: time.hpp:156
Definition: time.hpp:43
static std::string toString(const TimeType &tt)
Converts the time in this object to a human readable string.
Definition: time.cpp:69
static std::string getLogTime()
Get the time in string for game server logging prefix (thread-safe)
Definition: time.cpp:48
static uint64_t getMonoTimeMs()
Returns a time based since the starting of stk (monotonic clock).
Definition: time.hpp:106
static double getRealTime(long startAt=0)
Returns a time based on an arbitrary 'epoch' (e.g.
Definition: time.cpp:95
static void sleep(int msec)
Sleeps for the specified amount of time.
Definition: time.hpp:134
static std::chrono::steady_clock::time_point m_mono_start
Initalized when STK starts.
Definition: time.hpp:51
static irr::ITimer * m_timer
This objects keeps a copy of irrlicht's null-device timer.
Definition: time.hpp:48
static void init()
Init function for the timer.
Definition: time.cpp:39
static int compareTime(TimeType time1, TimeType time2)
Compare two different times.
Definition: time.hpp:118
static void getDate(int *day=NULL, int *month=NULL, int *year=NULL)
Returns the current date.
Definition: time.cpp:107
static TimeType getTimeSinceEpoch()
Returns the number of seconds since 1.1.1970.
Definition: time.hpp:68
static TimeType addInterval(TimeType time, int year, int month, int day)
Add a interval to a time.
Definition: time.hpp:146
Declares the general types that are used by the network.