1 2 /* 3 * Copyright (C) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SNTP_CLIENT_NTP_TRUSTED_TIME_H 18 #define SNTP_CLIENT_NTP_TRUSTED_TIME_H 19 20 #include "time_common.h" 21 22 namespace OHOS { 23 namespace MiscServices { 24 class NtpTrustedTime { 25 public: 26 static NtpTrustedTime &GetInstance(); 27 bool ForceRefresh(const std::string &ntpServer); 28 int64_t GetCacheAge(); 29 int64_t CurrentTimeMillis(); 30 int64_t ElapsedRealtimeMillis(); 31 std::chrono::steady_clock::time_point GetBootTimeNs(); 32 class TimeResult : std::enable_shared_from_this<TimeResult> { 33 public: 34 TimeResult(); 35 TimeResult(int64_t mTimeMillis, int64_t mElapsedRealtimeMills, int64_t mCertaintyMillis); 36 ~TimeResult(); 37 int64_t GetTimeMillis(); 38 int64_t GetElapsedRealtimeMillis(); 39 int64_t CurrentTimeMillis(); 40 int64_t GetAgeMillis(); 41 void Clear(); 42 43 private: 44 // Calculated network time from NTP server. 45 int64_t mTimeMillis; 46 // Boot time when getting time from NTP server. 47 int64_t mElapsedRealtimeMillis; 48 int64_t mCertaintyMillis; 49 }; 50 51 private: 52 std::shared_ptr<TimeResult> mTimeResult {}; 53 }; 54 } // namespace MiscServices 55 } // namespace OHOS 56 57 #endif // SNTP_CLIENT_NTP_TRUSTED_TIME_H