• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "time_common.h"
17 
18 
19 namespace OHOS {
20 namespace MiscServices {
21 namespace {
22 static constexpr int MILLI_TO_SEC = 1000LL;
23 static constexpr int NANO_TO_SEC = 1000000000LL;
24 constexpr int32_t NANO_TO_MILLI = NANO_TO_SEC / MILLI_TO_SEC;
25 }
26 
GetWallTimeMs(int64_t & time)27 int32_t TimeUtils::GetWallTimeMs(int64_t &time)
28 {
29     struct timespec tv {};
30     if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
31         TIME_HILOGE(TIME_MODULE_CLIENT, "get rt failed");
32         return E_TIME_SA_DIED;
33     }
34     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
35     return E_TIME_OK;
36 }
37 
GetBootTimeNs(int64_t & time)38 int32_t TimeUtils::GetBootTimeNs(int64_t &time)
39 {
40     struct timespec tv {};
41     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
42         TIME_HILOGE(TIME_MODULE_CLIENT, "get bt failed");
43         return E_TIME_SA_DIED;
44     }
45     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
46     return E_TIME_OK;
47 }
48 
49 // LCOV_EXCL_START
50 // The method has no input parameters, impossible to construct fuzz test.
GetBootTimeNs()51 std::chrono::steady_clock::time_point TimeUtils::GetBootTimeNs()
52 {
53     int64_t timeNow = -1;
54     struct timespec tv {};
55     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
56         return std::chrono::steady_clock::now();
57     }
58     timeNow = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
59     std::chrono::steady_clock::time_point tp_epoch ((std::chrono::nanoseconds(timeNow)));
60     return tp_epoch;
61 }
62 // LCOV_EXCL_STOP
63 
GetBootTimeMs(int64_t & time)64 int32_t TimeUtils::GetBootTimeMs(int64_t &time)
65 {
66     struct timespec tv {};
67     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
68         TIME_HILOGE(TIME_MODULE_CLIENT, "get bt failed");
69         return E_TIME_SA_DIED;
70     }
71     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
72     return E_TIME_OK;
73 }
74 
GetTimeByClockId(clockid_t clockId,struct timespec & tv)75 bool TimeUtils::GetTimeByClockId(clockid_t clockId, struct timespec &tv)
76 {
77     if (clock_gettime(clockId, &tv) < 0) {
78         TIME_HILOGE(TIME_MODULE_CLIENT, "Failed clock_gettime, errno: %{public}s", strerror(errno));
79         return false;
80     }
81     return true;
82 }
83 
84 }
85 }