diff --git a/services/include/form_timer_mgr.h b/services/include/form_timer_mgr.h index 5c61fc675a238e448da001713ec8ed526c7c42cf..6add4edcffb98cb574bbbb597f0c176aed8f5dad 100644 --- a/services/include/form_timer_mgr.h +++ b/services/include/form_timer_mgr.h @@ -350,6 +350,8 @@ private: } } CompareDynamicRefreshItem; + std::chrono::steady_clock::time_point GetBootTimeNs(); + mutable std::recursive_mutex intervalMutex_; mutable std::recursive_mutex updateAtMutex_; mutable std::recursive_mutex dynamicMutex_; diff --git a/services/src/form_timer_mgr.cpp b/services/src/form_timer_mgr.cpp index 437f6092bc4d1e3818080efe8502602b9784643f..d1aa75c97f5f9c1f294119a4093d3e1f1efc791c 100644 --- a/services/src/form_timer_mgr.cpp +++ b/services/src/form_timer_mgr.cpp @@ -37,6 +37,7 @@ const int REQUEST_UPDATE_AT_CODE = 1; const int REQUEST_LIMITER_CODE = 2; const int REQUEST_DYNAMIC_CODE = 3; const int SHIFT_BIT_LENGTH = 32; +const int NANO_TO_SECOND = 1000000000; const std::string FMS_TIME_SPEED = "fms.time_speed"; } // namespace @@ -898,7 +899,7 @@ bool FormTimerMgr::UpdateAtTimerAlarm() if (currentUpdateAtWantAgent_ != nullptr) { ClearUpdateAtTimerResource(); } - auto timeSinceEpoch = std::chrono::steady_clock::now().time_since_epoch(); + auto timeSinceEpoch = GetBootTimeNs().time_since_epoch(); int64_t timeInSec = std::chrono::duration_cast(timeSinceEpoch).count(); HILOG_DEBUG("timeInSec: %{public}" PRId64 ".", timeInSec); int64_t nextTime = timeInSec + (selectTime - currentTime); @@ -917,6 +918,19 @@ bool FormTimerMgr::UpdateAtTimerAlarm() return true; } +std::chrono::steady_clock::time_point FormTimerMgr::GetBootTimeNs() +{ + int64_t timeNow = -1; + struct timespec tv {}; + if (clock_gettime(CLOCK_BOOTTIME, &tv) < 0) { + HILOG_WARN("Get bootTime by clock_gettime failed, use std::chrono::steady_clock"); + return std::chrono::steady_clock::now(); + } + timeNow = tv.tv_sec * NANO_TO_SECOND + tv.tv_nsec; + std::chrono::steady_clock::time_point tp_epoch ((std::chrono::nanoseconds(timeNow))); + return tp_epoch; +} + /** * @brief Get WantAgent. * @param updateAtTime The next update time.