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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include "BasicLibTime.h"
#include "BasicLibString.h"
// ====================================================================================================================
// Include Files.
// ====================================================================================================================
#include <time.h>
#ifdef WIN32
#include "windows.h"
#else
#include <sys/time.h>
#endif
namespace BasicLib
{
// ================================================================================================================
// This is a hidden class, designed to initialize the win32 performance counter frequency value.
// ================================================================================================================
#ifdef WIN32
class Win32PerformanceCounter
{
public:
Win32PerformanceCounter()
{
// Get the "ticks per second" value.
QueryPerformanceFrequency((LARGE_INTEGER*)(&m_frequency));
// Convert it into a "ticks per millisecond" value.
m_frequency = m_frequency / 1000;
}
// This is the frequency of the performance counter, in "ticks per millisecond"
sint64 m_frequency;
};
Win32PerformanceCounter g_win32counter;
#endif
// ================================================================================================================
// These functions get a time value. The Actual meaning of this time is undefined; it is only meant to be relative.
// ================================================================================================================
sint64 GetTimeMS()
{
#ifdef WIN32
sint64 t;
QueryPerformanceCounter((LARGE_INTEGER*)(&t));
return t / g_win32counter.m_frequency;
#else
struct timeval t;
sint64 s;
// Get the time of day.
gettimeofday(&t, 0);
// Calculate the number of milliseconds represented by the seconds.
s = t.tv_sec;
s *= 1000;
// Calculate the milliseconds now.
s += (t.tv_usec / 1000);
// Return the value.
return s;
#endif
}
sint64 GetTimeS()
{
return GetTimeMS() / 1000;
}
sint64 GetTimeM()
{
return GetTimeMS() / 60000;
}
sint64 GetTimeH()
{
return GetTimeMS() / 3600000;
}
// ================================================================================================================
// This prints a timestamp in 24 hours hh:mm:ss format.
// ================================================================================================================
std::string TimeStamp()
{
char str[9];
// Get the time, and convert it to struct tm format.
time_t a = time(0);
struct tm* b = localtime(&a);
// Print the time to the string.
strftime(str, 9, "%H:%M:%S", b);
return str;
}
// ================================================================================================================
// This prints a datestamp in YYYY:MM:DD format.
// ================================================================================================================
std::string DateStamp()
{
char str[11];
// Get the time, and convert it to struct tm format.
time_t a = time(0);
struct tm* b = localtime(&a);
// Print the time to the string.
strftime(str, 11, "%Y.%m.%d", b);
return str;
}
Timer::Timer()
{
m_starttime = 0;
m_inittime = 0;
}
void Timer::Reset(sint64 p_timepassed)
{
m_starttime = p_timepassed;
m_inittime = GetTimeMS();
}
sint64 Timer::GetMS()
{
// Return the amount of time that has elapsed since the timer was initialized, plus whatever starting time the
// timer was given.
return (GetTimeMS() - m_inittime) + m_starttime;
}
sint64 Timer::GetS()
{
return GetMS() / 1000;
}
sint64 Timer::GetM()
{
return GetMS() / 60000;
}
sint64 Timer::GetH()
{
return GetMS() / 3600000;
}
sint64 Timer::GetD()
{
return GetMS() / 86400000;
}
sint64 Timer::GetY()
{
return GetD() / 365;
}
std::string Timer::GetString()
{
std::string str;
sint64 y = GetY();
sint64 d = GetD() % 365;
sint64 h = GetH() % 24;
sint64 m = GetM() % 60;
if (y > 0)
str += BasicLib::tostring(y) + " years, ";
if (d > 0)
str += BasicLib::tostring(d) + " days, ";
if (h > 0)
str += BasicLib::tostring(h) + " hours, ";
str += BasicLib::tostring(m) + " minutes";
return str;
}
} // End namespace BasicLib
|