-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexTimeCounter.h
More file actions
141 lines (118 loc) · 3.13 KB
/
HexTimeCounter.h
File metadata and controls
141 lines (118 loc) · 3.13 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
#ifndef __HEX_TIMECOUNTER_H
#define __HEX_TIMECOUNTER_H
#include "GetTime.h"
#include "HexString.h"
class HexTimeCounter
{
public:
HexTimeCounter();
~HexTimeCounter();
HexTime StartTimer();
HexTime StopTimer();
HexTime GetTimeSlapped();
DataStructures::HexString GetTimeSlappedString();
inline bool IsTimerStopped() { return timerStopped; }
bool StopTimerAtThePoint(HexTime ms);
bool AcquireTimerState(HexTime ms);
void ForceDecreaseLastTime(HexTime newTime);
void ForceIncreaseLastTime(HexTime newTime);
private:
HexTime currentTime, lastTime;
bool timerStopped;
};
class HexScaledTimeCounter
{
public:
HexScaledTimeCounter();
~HexScaledTimeCounter();
HexTime StartTimer();
HexTime StopTimer();
HexTime GetTimeSlapped();
DataStructures::HexString GetTimeSlappedString();
inline bool IsTimerStopped() { return timerStopped; }
bool StopTimerAtThePoint(HexTime ms);
bool AcquireTimerState(HexTime ms);
void ForceDecreaseLastTime(HexTime newTime);
void ForceIncreaseLastTime(HexTime newTime);
void SetTimeScale(float scale);
float GetTimeScale() { return mTimeScale; }
private:
HexTime currentTime, lastTime;
bool timerStopped;
float mTimeScale;
};
//------------------------------ time utility functions -----------------------------------
static __time_t UNINITIALIZED_TIME_T = 0;
static struct tm UNINITIALIZED_TIME_TM = *__localtime(&UNINITIALIZED_TIME_T);
inline bool IsUninitializedTime(struct tm &time)
{
return __mktime(&time) == UNINITIALIZED_TIME_T;
}
inline bool TimeEquals(struct tm &t1, struct tm &t2)
{
return memcmp(&t1, &t2, sizeof(struct tm)) == 0;
}
inline struct tm *GetUninitializedTime()
{
return &UNINITIALIZED_TIME_TM;
}
inline struct tm *GetCurrentTimeInTM()
{
__time_t long_time;
__time( &long_time );
return __localtime( &long_time ); // C4996
}
inline struct tm *GetExpireTimeInTM(unsigned int afterSecond)
{
__time_t nowtime;
__time(&nowtime);
nowtime += afterSecond;
return __localtime(&nowtime);
}
inline __time_t CompareTimeTM(struct tm *baseTM,struct tm *compareTM)
{
__time_t baseTMsecond = __mktime(baseTM);
__time_t compareTMsecond = __mktime(compareTM);
return compareTMsecond - baseTMsecond;
}
inline bool IsTimeExpired(struct tm *baseTime, int expireSeconds)
{
__time_t long_time;
__time( &long_time );
__time_t baseTime32 = __mktime(baseTime);
return long_time - baseTime32 >= expireSeconds;
}
inline __time_t GetDurationInSecond(__time_t from)
{
__time_t nowtime = __time(&nowtime);
nowtime -= from;
return nowtime;
}
inline __time_t GetDurationInSecond(__time_t from, __time_t to)
{
return to - from;
}
inline __time_t GetDurationInSecond(struct tm *from)
{
__time_t baseTMsecond = __mktime(from);
__time_t nowtime;
__time(&nowtime);
nowtime -= baseTMsecond;
return nowtime;
}
inline __time_t GetDurationInSecond(struct tm *from, struct tm *to)
{
__time_t baseTMsecond = __mktime(from);
__time_t toTime = __mktime(to);
toTime -= baseTMsecond;
return toTime;
}
inline __time_t64 GetDurationInMS(struct tm *from)
{
return GetDurationInSecond(from) * 1000;
}
inline __time_t64 GetDurationInMS(struct tm *from, struct tm *to)
{
return GetDurationInSecond(from, to) * 1000;
}
#endif