diff --git a/services/time/include/ntp_update_time.h b/services/time/include/ntp_update_time.h index 626f5ba901f559fb64c25faec3895e21a1b8c089..a4008fedcaa37b64ad9df2b87fe6c6b9107d9fc6 100644 --- a/services/time/include/ntp_update_time.h +++ b/services/time/include/ntp_update_time.h @@ -26,6 +26,11 @@ struct AutoTimeInfo { std::string status; int64_t lastUpdateTime; }; +enum NtpRefreshCode { + NO_NEED_REFRESH, + REFRESH_SUCCESS, + REFRESH_FAILED +}; class NtpUpdateTime { public: @@ -43,7 +48,8 @@ public: private: NtpUpdateTime(); - static bool GetNtpTimeInner(); + static NtpRefreshCode GetNtpTimeInner(); + static bool CheckNeedSetTime(NtpRefreshCode code, int64_t time); static bool GetRealTimeInner(int64_t &time); static void ChangeNtpServerCallback(const char *key, const char *value, void *context); static std::vector SplitNtpAddrs(const std::string &ntpStr); diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index b220a6e32e67c02a3c343c83112571dc1cde1cde..9088d9a4996e4cd818fa1b1a30a12a0059e5d77f 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace MiscServices { namespace { constexpr int64_t NANO_TO_MILLISECOND = 1000000; -constexpr int64_t SECOND_TO_MILLISECOND = 1000; +constexpr int64_t TWO_SECOND_TO_MILLISECOND = 2000; constexpr int64_t HALF_DAY_TO_MILLISECOND = 43200000; constexpr const char* NTP_SERVER_SYSTEM_PARAMETER = "persist.time.ntpserver"; constexpr const char* NTP_SERVER_SPECIFIC_SYSTEM_PARAMETER = "persist.time.ntpserver_specific"; @@ -147,10 +147,10 @@ bool NtpUpdateTime::IsInUpdateInterval() return false; } -bool NtpUpdateTime::GetNtpTimeInner() +NtpRefreshCode NtpUpdateTime::GetNtpTimeInner() { if (IsInUpdateInterval()) { - return true; + return NO_NEED_REFRESH; } std::vector ntpSpecList = SplitNtpAddrs(autoTimeInfo_.ntpServerSpec); @@ -160,11 +160,11 @@ bool NtpUpdateTime::GetNtpTimeInner() for (size_t j = 0; j < ntpSpecList.size(); j++) { TIME_HILOGI(TIME_MODULE_SERVICE, "ntpServer is : %{public}s", ntpSpecList[j].c_str()); if (NtpTrustedTime::GetInstance().ForceRefresh(ntpSpecList[j])) { - return true; + return REFRESH_SUCCESS; } } } - return false; + return REFRESH_FAILED; } bool NtpUpdateTime::GetRealTimeInner(int64_t &time) @@ -183,11 +183,29 @@ bool NtpUpdateTime::GetRealTime(int64_t &time) return GetRealTimeInner(time); } +bool NtpUpdateTime::CheckNeedSetTime(NtpRefreshCode code, int64_t time) +{ + if (code == NO_NEED_REFRESH) { + int64_t currentWallTime = 0; + if (TimeUtils::GetWallTimeMs(currentWallTime) != ERR_OK) { + TIME_HILOGE(TIME_MODULE_SERVICE, "get walltime fail"); + return false; + } + + if (std::abs(currentWallTime - time) < TWO_SECOND_TO_MILLISECOND) { + TIME_HILOGW(TIME_MODULE_SERVICE, "no need to refresh time"); + return false; + } + } + return true; +} + bool NtpUpdateTime::GetNtpTime(int64_t &time) { std::lock_guard autoLock(requestMutex_); - if (!GetNtpTimeInner()) { + auto ret = GetNtpTimeInner(); + if (ret == REFRESH_FAILED) { TIME_HILOGE(TIME_MODULE_SERVICE, "get ntp time failed"); return false; } @@ -196,7 +214,7 @@ bool NtpUpdateTime::GetNtpTime(int64_t &time) return false; } - if (autoTimeInfo_.status == AUTO_TIME_STATUS_ON) { + if (autoTimeInfo_.status == AUTO_TIME_STATUS_ON && CheckNeedSetTime(ret, time)) { TimeSystemAbility::GetInstance()->SetTimeInner(time); } return true; @@ -214,7 +232,8 @@ void NtpUpdateTime::SetSystemTime() return; } - if (!GetNtpTimeInner()) { + auto ret = GetNtpTimeInner(); + if (ret == REFRESH_FAILED) { TIME_HILOGE(TIME_MODULE_SERVICE, "get ntp time failed"); requestMutex_.unlock(); return; @@ -226,15 +245,7 @@ void NtpUpdateTime::SetSystemTime() requestMutex_.unlock(); return; } - int64_t currentWallTime = 0; - if (TimeUtils::GetWallTimeMs(currentWallTime) != ERR_OK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "get walltime fail"); - requestMutex_.unlock(); - return; - } - - if (std::abs(currentWallTime - currentTime) < SECOND_TO_MILLISECOND) { - TIME_HILOGW(TIME_MODULE_SERVICE, "no need to refresh time"); + if (!CheckNeedSetTime(ret, currentTime)) { requestMutex_.unlock(); return; } diff --git a/test/unittest/service_test/src/time_client_test.cpp b/test/unittest/service_test/src/time_client_test.cpp index db0531c3ad9089cd233f82f3ff06b46ce1725de5..2b9ec9ac6ecf40881db7f1bc9c783cc4fb373a28 100644 --- a/test/unittest/service_test/src/time_client_test.cpp +++ b/test/unittest/service_test/src/time_client_test.cpp @@ -160,7 +160,7 @@ void TestNtpThread(const char *name) int64_t timeLater; auto errCodeRealTime = TimeServiceClient::GetInstance()->GetRealTimeMs(timeLater); EXPECT_EQ(errCodeRealTime, TimeError::E_TIME_OK); - EXPECT_GT(timeLater, time); + EXPECT_GE(timeLater, time); } /**