-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClock.cpp
More file actions
121 lines (105 loc) · 2.5 KB
/
Clock.cpp
File metadata and controls
121 lines (105 loc) · 2.5 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
#include "Clock.h"
#include <time.h>
#ifdef WIN32
#include <Windows.h>
#else
#include<sys/time.h>
#endif
MODULE_IMPL(nicehero::Clock)
namespace nicehero
{
#ifdef WIN32
typedef long long LONGLONG;
#define FACTOR (0x19db1ded53e8000LL)
static inline i64
systime()
{
LARGE_INTEGER x;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
x.HighPart = ft.dwHighDateTime;
x.LowPart = ft.dwLowDateTime;
x.QuadPart -= FACTOR; /* Add conversion factor for UNIX vs. Windows base time */
x.QuadPart /= 10; /* Convert to microseconds */
return x.QuadPart;
}
int gettimeofday(struct ntimeval *tv, struct timezone *tz)
{
LONGLONG now = systime();
tv->tv_sec = static_cast<time_t>(now / 1000000);
tv->tv_usec = static_cast<long>(now % 1000000);
return 0;
}
#endif
Clock::Clock()
{
m_scale = 1.0;
#ifdef WIN32
m_millisecond = m_tailMillisecond = timeGetTime();
#endif
}
ui64 Clock::getTime()
{
return time(0);
}
ui64 Clock::getTimeMS()
{
return time(0) * 1000;
}
ui64 Clock::getSeconds()
{
#ifdef WIN32
gettimeofday(&m_current, NULL);
#else
::timeval current;
gettimeofday(¤t, NULL);
m_current.tv_sec = current.tv_sec;
m_current.tv_usec = current.tv_usec;
#endif
#ifdef WIN32
ui32 now = timeGetTime();
ui32 up = now - m_tailMillisecond;
m_tailMillisecond = now;
m_millisecond += up;
m_second = static_cast<ui32>(m_millisecond / 1000);
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
m_second = ts.tv_sec;
m_millisecond = (i64)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
#endif
m_currentMS = m_current.tv_sec * 1000;
m_currentMS += m_current.tv_usec / 1000;
m_retMillisecond = m_millisecond;// *m_scale;
m_retSecond = static_cast<ui32>(m_retMillisecond / 1000);
return m_retSecond;
}
ui64 Clock::getMilliSeconds()
{
#ifdef WIN32
gettimeofday(&m_current, NULL);
#else
::timeval current;
gettimeofday(¤t, NULL);
m_current.tv_sec = current.tv_sec;
m_current.tv_usec = current.tv_usec;
#endif
#ifdef WIN32
ui32 now = timeGetTime();
ui32 up = now - m_tailMillisecond;
m_tailMillisecond = now;
m_millisecond += up;
m_second = static_cast<ui32>(m_millisecond / 1000);
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
m_second = ts.tv_sec;
m_millisecond = (i64)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
#endif
m_currentMS = m_current.tv_sec * 1000;
m_currentMS += m_current.tv_usec / 1000;
m_retMillisecond = m_millisecond;// *m_scale;
m_retSecond = static_cast<ui32>(m_retMillisecond / 1000);
return m_retMillisecond;
}
}