Skip to content

Commit

Permalink
去除LLBC_Time中的_gmtTimeStruct成员及相应代码 #270
Browse files Browse the repository at this point in the history
  • Loading branch information
lailongwei committed Sep 19, 2024
1 parent f148233 commit 4339682
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
7 changes: 3 additions & 4 deletions llbc/include/llbc/core/time/Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LLBC_EXPORT LLBC_Time final
/**
* Destructor.
*/
~LLBC_Time();
~LLBC_Time() = default;

public:
/**
Expand Down Expand Up @@ -158,9 +158,9 @@ class LLBC_EXPORT LLBC_Time final
/**
* Get GMT time struct.
* @param[out] timeStruct - time struct object reference.
* @return const tm & - time struct object.
* @return tm & - time struct object.
*/
const tm &GetGmtTime() const;
tm GetGmtTime() const;
void GetGmtTime(tm &timeStruct) const;

/**
Expand Down Expand Up @@ -356,7 +356,6 @@ class LLBC_EXPORT LLBC_Time final

private:
sint64 _time;
tm _gmtTimeStruct;
tm _localTimeStruct;
};

Expand Down
32 changes: 22 additions & 10 deletions llbc/include/llbc/core/time/TimeInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@
__LLBC_NS_BEGIN

inline LLBC_Time::LLBC_Time()
: _time(0)
: LLBC_Time(0)
{
UpdateTimeStructs();
}

inline LLBC_Time::LLBC_Time(const LLBC_Time &time)
: _time(time._time)
{
memcpy(&_localTimeStruct, &time._localTimeStruct, sizeof(tm));
memcpy(&_gmtTimeStruct, &time._gmtTimeStruct, sizeof(tm));
}

inline LLBC_Time::~LLBC_Time()
{
}

inline LLBC_Time LLBC_Time::FromSeconds(time_t clanderTimeInSeconds)
Expand Down Expand Up @@ -152,14 +146,22 @@ inline sint64 LLBC_Time::GetTimestampInMicros() const
return _time;
}

inline const tm &LLBC_Time::GetGmtTime() const
inline tm LLBC_Time::GetGmtTime() const
{
return _gmtTimeStruct;
struct tm timeStruct;
GetGmtTime(timeStruct);

return timeStruct;
}

inline void LLBC_Time::GetGmtTime(tm &timeStruct) const
{
memcpy(&timeStruct, &_gmtTimeStruct, sizeof(tm));
const time_t calendarTime = static_cast<time_t>(_time / LLBC_TimeConst::numOfMicrosPerSecond);
#if LLBC_TARGET_PLATFORM_WIN32
gmtime_s(&timeStruct, &calendarTime);
#else
gmtime_r(&calendarTime, &timeStruct);
#endif
}

inline const tm &LLBC_Time::GetLocalTime() const
Expand Down Expand Up @@ -259,4 +261,14 @@ inline LLBC_Time::LLBC_Time(const sint64 &clendarTimeInMicroseconds)
UpdateTimeStructs();
}

inline void LLBC_Time::UpdateTimeStructs()
{
time_t calendarTime = static_cast<time_t>(_time / LLBC_TimeConst::numOfMicrosPerSecond);
#if LLBC_TARGET_PLATFORM_WIN32
localtime_s(&_localTimeStruct, &calendarTime);
#else
localtime_r(&calendarTime, &_localTimeStruct);
#endif
}

__LLBC_NS_END
19 changes: 4 additions & 15 deletions llbc/src/core/time/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ LLBC_String LLBC_Time::Format(const char *format) const
LLBC_String LLBC_Time::FormatAsGmt(const char *format) const
{
char buf[32];
struct tm gmtTimeStruct;
GetGmtTime(gmtTimeStruct);
if (format)
strftime(buf, sizeof(buf), format, &_gmtTimeStruct);
strftime(buf, sizeof(buf), format, &gmtTimeStruct);
else
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &_gmtTimeStruct);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &gmtTimeStruct);

return buf;
}
Expand Down Expand Up @@ -324,7 +326,6 @@ LLBC_Time &LLBC_Time::operator=(const LLBC_Time &time)

_time = time._time;
memcpy(&_localTimeStruct, &time._localTimeStruct, sizeof(tm));
memcpy(&_gmtTimeStruct, &time._gmtTimeStruct, sizeof(tm));

return *this;
}
Expand Down Expand Up @@ -514,18 +515,6 @@ LLBC_Time LLBC_Time::FromTimeStr(const char *timeStr, size_t timeStrLen)
#undef __LLBC_INL_TIME_STR_PART_TO_VAL
#undef __LLBC_INL_TIME_PARSE_SEPARATORS

void LLBC_Time::UpdateTimeStructs()
{
time_t calendarTime = static_cast<time_t>(_time / LLBC_TimeConst::numOfMicrosPerSecond);
#if LLBC_TARGET_PLATFORM_WIN32
localtime_s(&_localTimeStruct, &calendarTime);
gmtime_s(&_gmtTimeStruct, &calendarTime);
#else
localtime_r(&calendarTime, &_localTimeStruct);
gmtime_r(&calendarTime, &_gmtTimeStruct);
#endif
}

LLBC_TimeSpan LLBC_Time::GetIntervalTo(const LLBC_TimeSpan &timeCycle,
LLBC_TimeSpan toTimeOfTimeCycle) const
{
Expand Down
2 changes: 1 addition & 1 deletion testsuite/core/time/TestCase_Core_Time_Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void TestCase_Core_Time_Time::TimeClassTest()
for (size_t i = 0; i < fromTimeStrPerfTestTimes; ++i)
{
for (size_t j = 0; j < testTimeStrs.size(); ++j)
auto time = LLBC_Time::FromTimeStr(testTimeStrs[j]);
LLBC_Time::FromTimeStr(testTimeStrs[j]);
}
const auto costTime = LLBC_GetMicroseconds() - begTime;
std::cout << "LLBC_Time::FromTimeStr() perf test finished, "
Expand Down

0 comments on commit 4339682

Please sign in to comment.