-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTime.c
324 lines (268 loc) · 7.65 KB
/
SimpleTime.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
////////////////////////////////////////////////////////////////////////////////
//
// Global including files.
//
// Including the files for the system.
//
// These including files are generally used in different operation system.
//
////////////////////////////////////////////////////////////////////////////////
#include "Global.h"
#include <time.h>
////////////////////////////////////////////////////////////////////////////////
//
// Special including files
//
// Including the files for the specifal operation system.
//
// These including files are used for the specifal operation system.
// They are mainly concert about the management of thread, synchronization, socket etc.
//
////////////////////////////////////////////////////////////////////////////////
//For windows.
#ifdef _MICROSOFT_WINDOWS
#include <windows.h>
#endif
//For linux
#ifdef _REDHAT_LINUX
#include <sys/time.h>
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Retrieves the number of milliseconds.
//
////////////////////////////////////////////////////////////////////////////////
_UINT32 GetMilliseconds()
{
//For windows.
#ifdef _MICROSOFT_WINDOWS
//Get tick count.
return GetTickCount();
#endif
//For linux.
#ifdef _REDHAT_LINUX
struct timeval current;
struct timezone zone;
//Get current.
gettimeofday(¤t,&zone);
//Calculate millionsecond.
return current.tv_sec * 1000 + current.tv_usec / 1000;
#endif
}
////////////////////////////////////////////////////////////////////////////////
//
// Get performance counters.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL GetPerformanceCounters(_UINT32* lpPerformanceCounters)
{
//For windows.
#ifdef _MICROSOFT_WINDOWS
//Large integer.
LARGE_INTEGER largeCurrent;
#ifdef _DEBUG
assert(lpPerformanceCounters != NULL);
#endif
//Get current counter.
if(QueryPerformanceCounter(&largeCurrent))
{
//Get high part.
lpPerformanceCounters[0] = largeCurrent.HighPart;
//Get lower part.
lpPerformanceCounters[1] = largeCurrent.LowPart;
//Return true.
return _TRUE;
}
#endif
//For linux.
#ifdef _REDHAT_LINUX
struct timeval current;
struct timezone zone;
//Get current time.
if(gettimeofday(¤t,&zone) == _SUCCESS)
{
//Set high part.
lpPerformanceCounters[0] = current.tv_sec;
//Set lower part.
lpPerformanceCounters[1] = current.tv_usec;
//Return true.
return _TRUE;
}
#endif
#ifdef _DEBUG
PrintLine(stderr,"Time::GetPerformanceCounters : fail to get performance counters !");
#endif
//Return false.
return _FALSE;
}
////////////////////////////////////////////////////////////////////////////////
//
// Initialize time.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL InitializeTime(SimpleTime* pTime)
{
struct tm* pToday = NULL;
time_t timenow = 0;
#ifdef _DEBUG
assert(pTime != NULL);
#endif
//Clear time.
memset(pTime,0,sizeof(SimpleTime));
//Get timenow.
time(&timenow);
//Get localtime.
pToday = localtime(&timenow);
//Check result.
if(pToday == NULL)
{
#ifdef _DEBUG
PrintLine(stderr,"Time::InitializeTime : fail to get local time !");
#endif
return _FALSE;
}
//Get date values.
pTime->nDateValues[0] = pToday->tm_year + 1900;
pTime->nDateValues[1] = pToday->tm_mon + 1;
pTime->nDateValues[2] = pToday->tm_mday;
//Get time values.
pTime->nTimeValues[0] = pToday->tm_hour;
pTime->nTimeValues[1] = pToday->tm_min;
pTime->nTimeValues[2] = pToday->tm_sec;
//Get milliseconds.
pTime->nMilliseconds = GetMilliseconds();
//Get performance counters.
if(!GetPerformanceCounters(pTime->nPerformanceCounters))
{
#ifdef _DEBUG
PrintLine(stderr,"Time::InitializeTime : fail to get performance count !");
#endif
return _FALSE;
}
//Return true.
return _TRUE;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get date format.
//
////////////////////////////////////////////////////////////////////////////////
_STRING FormatDate(SimpleTime* pTime,_STRING lpstrSplitter)
{
#ifdef _DEBUG
assert(pTime != NULL && lpstrSplitter != NULL);
#endif
sprintf(pTime->strFormat,"%04d%s%02d%s%02d",
pTime->nDateValues[0],lpstrSplitter,pTime->nDateValues[1],lpstrSplitter,pTime->nDateValues[2]);
//Return format.
return pTime->strFormat;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get time format.
//
////////////////////////////////////////////////////////////////////////////////
_STRING FormatTime(SimpleTime* pTime,_STRING lpstrSplitter)
{
#ifdef _DEBUG
assert(pTime != NULL && lpstrSplitter != NULL);
#endif
sprintf(pTime->strFormat,"%02d%s%02d%s%02d",
pTime->nTimeValues[0],lpstrSplitter,pTime->nTimeValues[1],lpstrSplitter,pTime->nTimeValues[2]);
//Return format.
return pTime->strFormat;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get compact format.
//
////////////////////////////////////////////////////////////////////////////////
_STRING GetCompactFormat(SimpleTime* pTime)
{
#ifdef _DEBUG
assert(pTime != NULL);
#endif
sprintf(pTime->strFormat,"%04d%02d%02d%02d%02d%02d",
pTime->nDateValues[0],pTime->nDateValues[1],pTime->nDateValues[2],
pTime->nTimeValues[0],pTime->nTimeValues[1],pTime->nTimeValues[2]);
//Return format.
return pTime->strFormat;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get standard format.
//
////////////////////////////////////////////////////////////////////////////////
_STRING GetStandardFormat(SimpleTime* pTime)
{
#ifdef _DEBUG
assert(pTime != NULL);
#endif
sprintf(pTime->strFormat,"%04d-%02d-%02d %02d:%02d:%02d",
pTime->nDateValues[0],pTime->nDateValues[1],pTime->nDateValues[2],
pTime->nTimeValues[0],pTime->nTimeValues[1],pTime->nTimeValues[2]);
//Return format.
return pTime->strFormat;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get Recorder format.
//
////////////////////////////////////////////////////////////////////////////////
_STRING GetRecorderFormat(SimpleTime* pTime)
{
#ifdef _DEBUG
assert(pTime != NULL);
#endif
sprintf(pTime->strFormat,"#%04d-%02d-%02d %02d:%02d:%02d>%08x:%08x>%u",
pTime->nDateValues[0],pTime->nDateValues[1],pTime->nDateValues[2],
pTime->nTimeValues[0],pTime->nTimeValues[1],pTime->nTimeValues[2],
pTime->nPerformanceCounters[0],pTime->nPerformanceCounters[1],
pTime->nMilliseconds);
//Return format.
return pTime->strFormat;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get SMPP validity period.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL GetSMPPValidityPeriod(_INT32 nSecond,_STRING lpstrValidityPeriod)
{
SimpleTime future;
struct tm* pToday = NULL;
time_t timenow = 0;
#ifdef _DEBUG
assert(lpstrValidityPeriod != NULL);
#endif
//Clear time.
memset(&future,0,sizeof(SimpleTime));
//Get timenow.
time(&timenow);
//Add time.
timenow += nSecond;
//Get localtime.
pToday = localtime(&timenow);
//Check result.
if(pToday == NULL)
{
#ifdef _DEBUG
PrintLine(stderr,"Time::GetSMPPValidityPeriod : fail to get local time !");
#endif
return _FALSE;
}
//Get date values.
future.nDateValues[0] = (pToday->tm_year + 1900) % 100;
future.nDateValues[1] = pToday->tm_mon + 1;
future.nDateValues[2] = pToday->tm_mday;
//Get time values.
future.nTimeValues[0] = pToday->tm_hour;
future.nTimeValues[1] = pToday->tm_min;
future.nTimeValues[2] = pToday->tm_sec;
//Format time.
sprintf(lpstrValidityPeriod,"%02d%02d%02d%02d%02d%02d",
future.nDateValues[0],future.nDateValues[1],future.nDateValues[2],
future.nTimeValues[0],future.nTimeValues[1],future.nTimeValues[2]);
//Return true.
return _TRUE;
}