SuperTuxKart
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 
42 class StkTime
43 {
44 private:
48  static irr::ITimer *m_timer;
49 
51  static std::chrono::steady_clock::time_point m_mono_start;
52 public:
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  // ------------------------------------------------------------------------
67  static std::string toString(int year, int month, int day);
68  // ------------------------------------------------------------------------
70  static std::string getDateFormat();
71  // ------------------------------------------------------------------------
75  static TimeType getTimeSinceEpoch()
76  {
77 #ifdef WIN32
78  FILETIME ft;
79  GetSystemTimeAsFileTime(&ft);
80  __int64 t = ft.dwHighDateTime;
81  t <<= 32;
82  t /= 10;
83  // The Unix epoch starts on Jan 1 1970. Need to subtract
84  // the difference in seconds from Jan 1 1601.
85 # if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
86 # define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
87 # else
88 # define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
89 # endif
90  t -= DELTA_EPOCH_IN_MICROSECS;
91 
92  t |= ft.dwLowDateTime;
93  // Convert to seconds since epoch
94  t /= 1000000UL;
95  return t;
96 #else
97  struct timeval tv;
98  gettimeofday(&tv, NULL);
99  return tv.tv_sec;
100 #endif
101  }; // getTimeSinceEpoch
102 
103  // ------------------------------------------------------------------------
108  static double getRealTime(long startAt=0);
109  // ------------------------------------------------------------------------
113  static uint64_t getMonoTimeMs()
114  {
115  auto duration = std::chrono::steady_clock::now() - m_mono_start;
116  auto value =
117  std::chrono::duration_cast<std::chrono::milliseconds>(duration);
118  return value.count();
119  }
120  // ------------------------------------------------------------------------
125  static int compareTime(TimeType time1, TimeType time2)
126  {
127  double diff = difftime(time1, time2);
128 
129  if (diff > 0)
130  return 1;
131  else if (diff < 0)
132  return -1;
133  else
134  return 0;
135  }; // compareTime
136 
137  // ------------------------------------------------------------------------
141  static void sleep(int msec)
142  {
143 #ifdef WIN32
144  Sleep(msec);
145 #else
146  usleep(msec*1000);
147 #endif
148  } // sleep
149  // ------------------------------------------------------------------------
153  static TimeType addInterval(TimeType time, int year, int month, int day) {
154  struct tm t = *gmtime(&time);
155  t.tm_year += year;
156  t.tm_mon += month;
157  t.tm_mday += day;
158  return mktime(&t);
159  } // addInterval
160 
161  // ------------------------------------------------------------------------
163  {
164  uint64_t m_time;
165  std::string m_name;
166  public:
167  ScopeProfiler(const char* name);
168  ~ScopeProfiler();
169  }; // class ScopeProfiler
170 
171 }; // namespace time
172 #endif
173 
Definition: time.hpp:163
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:113
static double getRealTime(long startAt=0)
Returns a time based on an arbitrary 'epoch' (e.g.
Definition: time.cpp:124
static void sleep(int msec)
Sleeps for the specified amount of time.
Definition: time.hpp:141
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 std::string getDateFormat()
Obtains the translated format of the time string.
Definition: time.cpp:102
static int compareTime(TimeType time1, TimeType time2)
Compare two different times.
Definition: time.hpp:125
static void getDate(int *day=NULL, int *month=NULL, int *year=NULL)
Returns the current date.
Definition: time.cpp:136
static TimeType getTimeSinceEpoch()
Returns the number of seconds since 1.1.1970.
Definition: time.hpp:75
static TimeType addInterval(TimeType time, int year, int month, int day)
Add a interval to a time.
Definition: time.hpp:153
Declares the general types that are used by the network.