Skip to content

Commit 3436094

Browse files
Fea, 添加DataTime类
1 parent 8ee3573 commit 3436094

File tree

4 files changed

+359
-0
lines changed

4 files changed

+359
-0
lines changed

UnitTest/UnitTest.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
<ClCompile Include="..\src\YY\Base\Threading\TaskRunnerDispatchImpl.cpp" />
182182
<ClCompile Include="..\src\YY\Base\Threading\TaskRunnerImpl.cpp" />
183183
<ClCompile Include="..\src\YY\Base\Threading\ThreadTaskRunnerImpl.cpp" />
184+
<ClCompile Include="..\src\YY\Base\Time\DataTime.cpp" />
184185
<ClCompile Include="..\src\YY\Base\Utils\FileInfo.cpp" />
185186
<ClCompile Include="..\src\YY\Base\Utils\SystemInfo.cpp" />
186187
<ClCompile Include="AsyncFileUnitTest.cpp" />
@@ -231,6 +232,7 @@
231232
<ClInclude Include="..\include\YY\Base\Time\Common.h" />
232233
<ClInclude Include="..\include\YY\Base\Time\TickCount.h" />
233234
<ClInclude Include="..\include\YY\Base\Time\TimeSpan.h" />
235+
<ClInclude Include="..\include\YY\Base\Time\DataTime.h" />
234236
<ClInclude Include="..\include\YY\Base\Utils\ComObjectImpl.h" />
235237
<ClInclude Include="..\include\YY\Base\Utils\FileInfo.h" />
236238
<ClInclude Include="..\include\YY\Base\Utils\MathUtils.h" />

UnitTest/UnitTest.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@
311311
<ClInclude Include="..\include\YY\Base\Functional\Bind.h">
312312
<Filter>头文件\YY\Base\Functional</Filter>
313313
</ClInclude>
314+
<ClInclude Include="..\include\YY\Base\Time\UtcTime.h">
315+
<Filter>头文件\YY\Base\Time</Filter>
316+
</ClInclude>
314317
</ItemGroup>
315318
<ItemGroup>
316319
<Natvis Include="..\src\YY.Base.natvis">

include/YY/Base/Time/DataTime.h

Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
#pragma once
2+
#include <YY/Base/YY.h>
3+
#include <YY/Base/Time/Common.h>
4+
#include <YY/Base/Time/TimeSpan.h>
5+
#include <YY/Base/Utils/MathUtils.h>
6+
#include <YY/Base/Containers/Optional.h>
7+
8+
#if defined(_HAS_CXX20) && _HAS_CXX20
9+
#include <compare>
10+
#endif
11+
12+
#pragma pack(push, __YY_PACKING)
13+
14+
namespace YY
15+
{
16+
namespace Base
17+
{
18+
namespace Time
19+
{
20+
class LocalDataTime;
21+
22+
class UtcDataTime
23+
{
24+
public:
25+
union
26+
{
27+
FILETIME oFileTime = {};
28+
uint64_t uTimeInternalValue;
29+
};
30+
31+
constexpr UtcDataTime() = default;
32+
33+
explicit constexpr UtcDataTime(const FILETIME& oFileTime)
34+
: oFileTime(oFileTime)
35+
{
36+
}
37+
38+
explicit UtcDataTime(const SYSTEMTIME& _oSystemTime)
39+
{
40+
SystemTimeToFileTime(&_oSystemTime, &oFileTime);
41+
}
42+
43+
static UtcDataTime __YYAPI GetNow()
44+
{
45+
UtcDataTime _oTime;
46+
GetSystemTimePreciseAsFileTime(&_oTime.oFileTime);
47+
return _oTime;
48+
}
49+
50+
constexpr static uint64_t __YYAPI GetSecondsPerInternal() noexcept
51+
{
52+
// 100纳秒间隔
53+
return SecondsPerMillisecond * MillisecondsPerMicrosecond * 10;
54+
}
55+
56+
static constexpr UtcDataTime __YYAPI FromFileTime(const FILETIME& _oFileTime)
57+
{
58+
UtcDataTime _oTime(_oFileTime);
59+
return _oTime;
60+
}
61+
62+
static UtcDataTime __YYAPI FromSystemTime(const SYSTEMTIME& _oSystemTime)
63+
{
64+
UtcDataTime _oTime(_oSystemTime);
65+
return _oTime;
66+
}
67+
68+
static constexpr UtcDataTime __YYAPI FromCrtTime(time_t _oCrtTime)
69+
{
70+
UtcDataTime _oTime;
71+
_oTime.uTimeInternalValue = uint64_t(_oCrtTime) * 10000000ULL + 116444736000000000ULL;
72+
return _oTime;
73+
}
74+
75+
SYSTEMTIME __YYAPI GetSystemTime() const
76+
{
77+
SYSTEMTIME _oSystemTime;
78+
FileTimeToSystemTime(&oFileTime, &_oSystemTime);
79+
return _oSystemTime;
80+
}
81+
82+
LocalDataTime __YYAPI GetLocalDataTime(_In_opt_ const DYNAMIC_TIME_ZONE_INFORMATION* _pTimeZoneInformation = nullptr) const;
83+
84+
constexpr time_t __YYAPI GetCrtTime() const
85+
{
86+
if (uTimeInternalValue < 116444736000000000ULL)
87+
return -1;
88+
89+
return (uTimeInternalValue - 116444736000000000ULL) / 10000000ULL;
90+
}
91+
92+
constexpr UtcDataTime& operator=(const UtcDataTime&) noexcept = default;
93+
94+
constexpr bool operator==(const UtcDataTime& _oOther) const noexcept
95+
{
96+
return uTimeInternalValue == _oOther.uTimeInternalValue;
97+
}
98+
99+
#if defined(_HAS_CXX20) && _HAS_CXX20
100+
constexpr auto operator<=>(const UtcDataTime& _oOther) const noexcept
101+
{
102+
return uTimeInternalValue <=> _oOther.uTimeInternalValue;
103+
}
104+
#else
105+
constexpr bool operator>(const UtcDataTime& _oOther) const noexcept
106+
{
107+
return uTimeInternalValue > _oOther.uTimeInternalValue;
108+
}
109+
110+
constexpr bool operator>=(const UtcDataTime& _oOther) const noexcept
111+
{
112+
return uTimeInternalValue >= _oOther.uTimeInternalValue;
113+
}
114+
115+
constexpr bool operator<=(const UtcDataTime& _oOther) const noexcept
116+
{
117+
return uTimeInternalValue <= _oOther.uTimeInternalValue;
118+
}
119+
120+
constexpr bool operator<(const UtcDataTime& _oOther) const noexcept
121+
{
122+
return uTimeInternalValue < _oOther.uTimeInternalValue;
123+
}
124+
125+
constexpr bool operator!=(const UtcDataTime& _oOther) const noexcept
126+
{
127+
return uTimeInternalValue != _oOther.uTimeInternalValue;
128+
}
129+
#endif
130+
131+
constexpr UtcDataTime& operator+=(const TimeSpan& _nSpan) noexcept
132+
{
133+
// uTimeInternalValue += _nSpan.GetInternalValue() * GetSecondsPerInternal() / TimeSpan::GetSecondsPerInternal();
134+
uTimeInternalValue += UMulDiv64Fast(_nSpan.GetInternalValue(), GetSecondsPerInternal(), TimeSpan::GetSecondsPerInternal());
135+
return *this;
136+
}
137+
138+
constexpr UtcDataTime operator+(const TimeSpan& _nSpan) noexcept
139+
{
140+
UtcDataTime _oTmp = *this;
141+
_oTmp += _nSpan;
142+
return _oTmp;
143+
}
144+
145+
constexpr UtcDataTime& operator-=(const TimeSpan& _nSpan) noexcept
146+
{
147+
// uTimeInternalValue -= _nSpan.GetInternalValue() * GetSecondsPerInternal() / TimeSpan::GetSecondsPerInternal();
148+
uTimeInternalValue -= UMulDiv64Fast(_nSpan.GetInternalValue(), GetSecondsPerInternal(), TimeSpan::GetSecondsPerInternal());
149+
return *this;
150+
}
151+
152+
constexpr UtcDataTime operator-(const TimeSpan& _nSpan) noexcept
153+
{
154+
UtcDataTime _oTmp = *this;
155+
_oTmp -= _nSpan;
156+
return _oTmp;
157+
}
158+
159+
constexpr TimeSpan operator-(const UtcDataTime& _oOther) const noexcept
160+
{
161+
int64_t _nSpanInternal = uTimeInternalValue - _oOther.uTimeInternalValue;
162+
// return TimeSpan::FromInternalValue(_nSpanInternal * TimeSpan::GetSecondsPerInternal() / int64_t(GetSecondsPerInternal()));
163+
return TimeSpan::FromInternalValue(MulDiv64Fast(_nSpanInternal, TimeSpan::GetSecondsPerInternal(), GetSecondsPerInternal()));
164+
}
165+
};
166+
167+
class LocalDataTime
168+
{
169+
public:
170+
union
171+
{
172+
FILETIME oFileTime = {};
173+
uint64_t uTimeInternalValue;
174+
};
175+
176+
constexpr LocalDataTime() = default;
177+
178+
explicit constexpr LocalDataTime(const FILETIME& oFileTime)
179+
: oFileTime(oFileTime)
180+
{
181+
}
182+
183+
explicit LocalDataTime(const SYSTEMTIME& _oSystemTime)
184+
{
185+
SystemTimeToFileTime(&_oSystemTime, &oFileTime);
186+
}
187+
188+
static LocalDataTime __YYAPI GetNow(_In_opt_ const DYNAMIC_TIME_ZONE_INFORMATION* _pTimeZoneInformation = nullptr)
189+
{
190+
return UtcDataTime::GetNow().GetLocalDataTime(_pTimeZoneInformation);
191+
}
192+
193+
constexpr static uint64_t __YYAPI GetSecondsPerInternal() noexcept
194+
{
195+
// 100纳秒间隔
196+
return SecondsPerMillisecond * MillisecondsPerMicrosecond * 10;
197+
}
198+
199+
static constexpr LocalDataTime __YYAPI FromFileTime(const FILETIME& _oFileTime) noexcept
200+
{
201+
LocalDataTime _oTime(_oFileTime);
202+
return _oTime;
203+
}
204+
205+
static LocalDataTime __YYAPI FromSystemTime(const SYSTEMTIME& _oSystemTime)
206+
{
207+
LocalDataTime _oTime(_oSystemTime);
208+
return _oTime;
209+
}
210+
211+
static constexpr LocalDataTime __YYAPI FromCrtTime(time_t _oCrtTime)
212+
{
213+
LocalDataTime _oTime;
214+
_oTime.uTimeInternalValue = uint64_t(_oCrtTime) * 10000000LL + 116444736000000000LL;
215+
return _oTime;
216+
}
217+
218+
SYSTEMTIME __YYAPI GetSystemTime() const
219+
{
220+
SYSTEMTIME _oSystemTime;
221+
FileTimeToSystemTime(&oFileTime, &_oSystemTime);
222+
return _oSystemTime;
223+
}
224+
225+
UtcDataTime __YYAPI GetUtcDataTime(_In_opt_ const DYNAMIC_TIME_ZONE_INFORMATION* _pTimeZoneInformation = nullptr) const;
226+
227+
constexpr time_t __YYAPI GetCrtTime() const
228+
{
229+
if (uTimeInternalValue < 116444736000000000ULL)
230+
return -1;
231+
232+
return (uTimeInternalValue - 116444736000000000ULL) / 10000000ULL;
233+
}
234+
235+
constexpr LocalDataTime& operator=(const LocalDataTime&) noexcept = default;
236+
237+
constexpr bool operator==(const LocalDataTime& _oOther) const noexcept
238+
{
239+
return uTimeInternalValue == _oOther.uTimeInternalValue;
240+
}
241+
242+
#if defined(_HAS_CXX20) && _HAS_CXX20
243+
constexpr auto operator<=>(const LocalDataTime& _oOther) const noexcept
244+
{
245+
return uTimeInternalValue <=> _oOther.uTimeInternalValue;
246+
}
247+
#else
248+
constexpr bool operator>(const LocalDataTime& _oOther) const noexcept
249+
{
250+
return uTimeInternalValue > _oOther.uTimeInternalValue;
251+
}
252+
253+
constexpr bool operator>=(const LocalDataTime& _oOther) const noexcept
254+
{
255+
return uTimeInternalValue >= _oOther.uTimeInternalValue;
256+
}
257+
258+
constexpr bool operator<=(const LocalDataTime& _oOther) const noexcept
259+
{
260+
return uTimeInternalValue <= _oOther.uTimeInternalValue;
261+
}
262+
263+
constexpr bool operator<(const LocalDataTime& _oOther) const noexcept
264+
{
265+
return uTimeInternalValue < _oOther.uTimeInternalValue;
266+
}
267+
268+
constexpr bool operator!=(const LocalDataTime& _oOther) const noexcept
269+
{
270+
return uTimeInternalValue != _oOther.uTimeInternalValue;
271+
}
272+
#endif
273+
274+
constexpr LocalDataTime& operator+=(const TimeSpan& _nSpan) noexcept
275+
{
276+
// uTimeInternalValue += _nSpan.GetInternalValue() * GetSecondsPerInternal() / TimeSpan::GetSecondsPerInternal();
277+
uTimeInternalValue += UMulDiv64Fast(_nSpan.GetInternalValue(), GetSecondsPerInternal(), TimeSpan::GetSecondsPerInternal());
278+
return *this;
279+
}
280+
281+
constexpr LocalDataTime operator+(const TimeSpan& _nSpan) noexcept
282+
{
283+
LocalDataTime _oTmp = *this;
284+
_oTmp += _nSpan;
285+
return _oTmp;
286+
}
287+
288+
constexpr LocalDataTime& operator-=(const TimeSpan& _nSpan) noexcept
289+
{
290+
// uTimeInternalValue -= _nSpan.GetInternalValue() * GetSecondsPerInternal() / TimeSpan::GetSecondsPerInternal();
291+
uTimeInternalValue -= UMulDiv64Fast(_nSpan.GetInternalValue(), GetSecondsPerInternal(), TimeSpan::GetSecondsPerInternal());
292+
return *this;
293+
}
294+
295+
constexpr LocalDataTime operator-(const TimeSpan& _nSpan) noexcept
296+
{
297+
LocalDataTime _oTmp = *this;
298+
_oTmp -= _nSpan;
299+
return _oTmp;
300+
}
301+
302+
constexpr TimeSpan operator-(const LocalDataTime& _oOther) const noexcept
303+
{
304+
int64_t _nSpanInternal = uTimeInternalValue - _oOther.uTimeInternalValue;
305+
// return TimeSpan::FromInternalValue(_nSpanInternal * TimeSpan::GetSecondsPerInternal() / int64_t(GetSecondsPerInternal()));
306+
return TimeSpan::FromInternalValue(MulDiv64Fast(_nSpanInternal, TimeSpan::GetSecondsPerInternal(), GetSecondsPerInternal()));
307+
}
308+
};
309+
}
310+
}
311+
}
312+
313+
namespace YY
314+
{
315+
using namespace YY::Base::Time;
316+
}
317+
318+
#pragma pack(pop)

src/YY/Base/Time/DataTime.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <YY/Base/Time/DataTime.h>
2+
3+
__YY_IGNORE_INCONSISTENT_ANNOTATION_FOR_FUNCTION()
4+
5+
namespace YY
6+
{
7+
namespace Base
8+
{
9+
namespace Time
10+
{
11+
LocalDataTime __YYAPI UtcDataTime::GetLocalDataTime(const DYNAMIC_TIME_ZONE_INFORMATION* _pTimeZoneInformation) const
12+
{
13+
auto _oSystemTime = GetSystemTime();
14+
SYSTEMTIME _oLocalSystemTime;
15+
SystemTimeToTzSpecificLocalTimeEx(_pTimeZoneInformation, &_oSystemTime, &_oLocalSystemTime);
16+
17+
LocalDataTime _oLocalDataTime(_oLocalSystemTime);
18+
// 修正转换过程中的精度损失
19+
_oLocalDataTime.uTimeInternalValue += uTimeInternalValue % (10 * MillisecondsPerMicrosecond);
20+
return _oLocalDataTime;
21+
}
22+
23+
UtcDataTime __YYAPI LocalDataTime::GetUtcDataTime(const DYNAMIC_TIME_ZONE_INFORMATION* _pTimeZoneInformation) const
24+
{
25+
auto _oSystemTime = GetSystemTime();
26+
SYSTEMTIME _oUtcSystemTime;
27+
TzSpecificLocalTimeToSystemTimeEx(_pTimeZoneInformation, &_oSystemTime, &_oUtcSystemTime);
28+
29+
UtcDataTime _oUtcDataTime(_oUtcSystemTime);
30+
// 修正转换过程中的精度损失
31+
_oUtcDataTime.uTimeInternalValue += uTimeInternalValue % (10 * MillisecondsPerMicrosecond);
32+
return _oUtcDataTime;
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)