soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
stime.cpp
1#include <souistd.h>
2#include <helper/STime.h>
3
4SNSBEGIN
5static const int kMaxTimeBufferSize = 128;
6
8 : m_timeSpan(0)
9{
10}
11
12STimeSpan::STimeSpan(__time64_t time)
13 : m_timeSpan(time)
14{
15}
16
17STimeSpan::STimeSpan(LONG lDays, int nHours, int nMins, int nSecs)
18{
19 m_timeSpan = nSecs + 60 * (nMins + 60 * (nHours + int64_t(24) * lDays));
20}
21
22LONGLONG STimeSpan::GetDays() const
23{
24 return (m_timeSpan / (24 * 3600));
25}
26
28{
29 return (m_timeSpan / 3600);
30}
31
33{
34 return (LONG(GetTotalHours() - (GetDays() * 24)));
35}
36
38{
39 return (m_timeSpan / 60);
40}
41
43{
44 return (LONG(GetTotalMinutes() - (GetTotalHours() * 60)));
45}
46
48{
49 return (m_timeSpan);
50}
51
53{
54 return (LONG(GetTotalSeconds() - (GetTotalMinutes() * 60)));
55}
56
57__time64_t STimeSpan::GetTimeSpan() const
58{
59 return (m_timeSpan);
60}
61
63{
64 return (STimeSpan(m_timeSpan + span.m_timeSpan));
65}
66
68{
69 return (STimeSpan(m_timeSpan - span.m_timeSpan));
70}
71
73{
74 m_timeSpan += span.m_timeSpan;
75 return (*this);
76}
77
79{
80 m_timeSpan -= span.m_timeSpan;
81 return (*this);
82}
83
85{
86 return (m_timeSpan == span.m_timeSpan);
87}
88
90{
91 return (m_timeSpan != span.m_timeSpan);
92}
93
95{
96 return (m_timeSpan < span.m_timeSpan);
97}
98
100{
101 return (m_timeSpan > span.m_timeSpan);
102}
103
105{
106 return (m_timeSpan <= span.m_timeSpan);
107}
108
110{
111 return (m_timeSpan >= span.m_timeSpan);
112}
113
114/////////////////////////////////////////////////////////////////////////
115
116STime::STime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST)
117{
118 SetDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
119}
120
121void STime::SetDateTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDst)
122{
123 struct tm atm;
124
125 atm.tm_sec = nSec;
126 atm.tm_min = nMin;
127 atm.tm_hour = nHour;
128 atm.tm_mday = nDay;
129 atm.tm_mon = nMonth - 1; // tm_mon is 0 based
130 atm.tm_year = nYear - 1900; // tm_year is 1900 based
131 atm.tm_isdst = nDst;
132
133 m_time = _mktime64(&atm);
134}
135
136void STime::SetDate(int nYear, int nMonth, int nDay)
137{
138 SetDateTime(nYear, nMonth, nDay, 0, 0, 0);
139}
140
141struct tm *STime::GetLocalTm(struct tm *ptm) const
142{
143 // Ensure ptm is valid
144 SASSERT(ptm != NULL);
145
146 if (ptm != NULL)
147 {
148 struct tm *tmTemp = localtime(&m_time);
149 if (!tmTemp)
150 {
151 return NULL; // indicates that m_time was not initialized!
152 }
153 *ptm = *tmTemp;
154 return ptm;
155 }
156
157 return NULL;
158}
159
160bool STime::GetAsSystemTime(SYSTEMTIME &timeDest) const
161{
162 struct tm ttm;
163 struct tm *ptm;
164
165 ptm = GetLocalTm(&ttm);
166
167 if (!ptm)
168 {
169 return false;
170 }
171
172 timeDest.wYear = (WORD)(1900 + ptm->tm_year);
173 timeDest.wMonth = (WORD)(1 + ptm->tm_mon);
174 timeDest.wDayOfWeek = (WORD)ptm->tm_wday;
175 timeDest.wDay = (WORD)ptm->tm_mday;
176 timeDest.wHour = (WORD)ptm->tm_hour;
177 timeDest.wMinute = (WORD)ptm->tm_min;
178 timeDest.wSecond = (WORD)ptm->tm_sec;
179 timeDest.wMilliseconds = 0;
180
181 return true;
182}
183
185{
186 return (STime(::_time64(NULL)));
187}
188
190{
191 SYSTEMTIME st;
192 GetSystemTime(&st); // We use UTC time
193
194 tm current;
195 memset(&current, 0, sizeof(current));
196
197 current.tm_year = st.wYear - 1900;
198 current.tm_mon = st.wMonth - 1;
199 current.tm_mday = st.wDay;
200 current.tm_hour = st.wHour;
201 current.tm_min = st.wMinute;
202 current.tm_sec = st.wSecond;
203 time_t currentTime = _mkgmtime(&current);
204 return static_cast<uint64_t>(currentTime) * 1000 + st.wMilliseconds;
205}
206
207__time64_t STime::GetTime() const
208{
209 return (m_time);
210}
211
212int STime::GetYear() const
213{
214 struct tm *ptm = localtime(&m_time);
215 return ptm ? (ptm->tm_year + 1900) : 0;
216}
217
219{
220 struct tm *ptm = localtime(&m_time);
221 return ptm ? (ptm->tm_mon + 1) : 0;
222}
223
224int STime::GetDay() const
225{
226 struct tm *ptm = localtime(&m_time);
227 return ptm ? ptm->tm_mday : 0;
228}
229
230int STime::GetHour() const
231{
232 struct tm *ptm = localtime(&m_time);
233 return ptm ? ptm->tm_hour : -1;
234}
235
237{
238 struct tm *ptm = localtime(&m_time);
239 return ptm ? ptm->tm_min : -1;
240}
241
243{
244 struct tm *ptm = localtime(&m_time);
245 return ptm ? ptm->tm_sec : -1;
246}
247
249{
250 struct tm *ptm = localtime(&m_time);
251 return ptm ? ptm->tm_wday + 1 : 0;
252}
253
254STime &STime::operator=(__time64_t time)
255{
256 m_time = time;
257
258 return (*this);
259}
260
261bool STime::operator==(STime time) const
262{
263 return (m_time == time.m_time);
264}
265
266bool STime::operator!=(STime time) const
267{
268 return (m_time != time.m_time);
269}
270
271bool STime::operator<(STime time) const
272{
273 return (m_time < time.m_time);
274}
275
276bool STime::operator>(STime time) const
277{
278 return (m_time > time.m_time);
279}
280
281bool STime::operator<=(STime time) const
282{
283 return (m_time <= time.m_time);
284}
285
286bool STime::operator>=(STime time) const
287{
288 return (m_time >= time.m_time);
289}
290
292{
293 m_time += span.GetTimeSpan();
294
295 return (*this);
296}
297
299{
300 m_time -= span.GetTimeSpan();
301
302 return (*this);
303}
304
306{
307 return (STimeSpan(m_time - time.m_time));
308}
309
311{
312 return (STime(m_time - span.GetTimeSpan()));
313}
314
316{
317 return (STime(m_time + span.GetTimeSpan()));
318}
319
320SStringT STime::Format(LPCTSTR pszFormat) const
321{
322 if (pszFormat == NULL)
323 {
324 return _T("");
325 }
326
327 TCHAR szBuffer[kMaxTimeBufferSize];
328
329 struct tm *tmTemp = localtime(&m_time);
330 if (tmTemp || !_tcsftime(szBuffer, kMaxTimeBufferSize, pszFormat, tmTemp))
331 {
332 szBuffer[0] = '\0';
333 }
334 return szBuffer;
335}
336SNSEND
int GetMinute() const
获取分钟
Definition stime.cpp:236
STime operator+(STimeSpan span) const
加法运算符重载
Definition stime.cpp:315
STime & operator-=(STimeSpan span)
减法赋值运算符重载
Definition stime.cpp:298
static STime GetCurrentTime()
获取当前时间
Definition stime.cpp:184
int GetYear() const
获取年份
Definition stime.cpp:212
STimeSpan operator-(STime time) const
减法运算符重载
Definition stime.cpp:305
bool operator>(STime time) const
大于运算符重载
Definition stime.cpp:276
int GetDay() const
获取日
Definition stime.cpp:224
static uint64_t GetCurrentTimeMs()
获取当前时间的毫秒数
Definition stime.cpp:189
int GetHour() const
获取小时
Definition stime.cpp:230
int GetMonth() const
获取月份
Definition stime.cpp:218
bool operator<=(STime time) const
小于等于运算符重载
Definition stime.cpp:281
int GetSecond() const
获取秒
Definition stime.cpp:242
STime & operator+=(STimeSpan span)
加法赋值运算符重载
Definition stime.cpp:291
bool GetAsSystemTime(SYSTEMTIME &timeDest) const
获取系统时间结构
Definition stime.cpp:160
bool operator>=(STime time) const
大于等于运算符重载
Definition stime.cpp:286
__time64_t GetTime() const
获取时间秒数
Definition stime.cpp:207
bool operator==(STime time) const
等于运算符重载
Definition stime.cpp:261
void SetDate(int nYear, int nMonth, int nDay)
设置日期
Definition stime.cpp:136
int GetDayOfWeek() const
获取星期几
Definition stime.cpp:248
SStringT Format(LPCTSTR pszFormat) const
格式化时间字符串
Definition stime.cpp:320
bool operator<(STime time) const
小于运算符重载
Definition stime.cpp:271
bool operator!=(STime time) const
不等于运算符重载
Definition stime.cpp:266
STime(__time64_t tm=0)
默认构造函数
Definition STime.h:178
void SetDateTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST=-1)
设置日期和时间
Definition stime.cpp:121
STime & operator=(__time64_t time)
赋值运算符重载
Definition stime.cpp:254
struct tm * GetLocalTm(struct tm *ptm) const
获取本地时间结构
Definition stime.cpp:141
时间间隔类
Definition STime.h:12
bool operator!=(STimeSpan span) const
不等于运算符重载
Definition stime.cpp:89
STimeSpan()
默认构造函数
Definition stime.cpp:7
LONGLONG GetTotalMinutes() const
获取总分钟数
Definition stime.cpp:37
LONGLONG GetDays() const
获取天数
Definition stime.cpp:22
LONG GetSeconds() const
获取秒数
Definition stime.cpp:52
bool operator<(STimeSpan span) const
小于运算符重载
Definition stime.cpp:94
LONGLONG GetTotalHours() const
获取总小时数
Definition stime.cpp:27
STimeSpan & operator-=(STimeSpan span)
减法赋值运算符重载
Definition stime.cpp:78
STimeSpan operator-(STimeSpan span) const
减法运算符重载
Definition stime.cpp:67
LONG GetMinutes() const
获取分钟数
Definition stime.cpp:42
bool operator>(STimeSpan span) const
大于运算符重载
Definition stime.cpp:99
bool operator==(STimeSpan span) const
等于运算符重载
Definition stime.cpp:84
LONG GetHours() const
获取小时数
Definition stime.cpp:32
bool operator>=(STimeSpan span) const
大于等于运算符重载
Definition stime.cpp:109
bool operator<=(STimeSpan span) const
小于等于运算符重载
Definition stime.cpp:104
STimeSpan operator+(STimeSpan span) const
加法运算符重载
Definition stime.cpp:62
__time64_t GetTimeSpan() const
获取时间间隔秒数
Definition stime.cpp:57
STimeSpan & operator+=(STimeSpan span)
加法赋值运算符重载
Definition stime.cpp:72
LONGLONG GetTotalSeconds() const
获取总秒数
Definition stime.cpp:47