• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "pasteboard_time.h"
17 
18 #include <sys/time.h>
19 
20 namespace OHOS {
21 namespace MiscServices {
22 namespace {
23 static constexpr uint64_t SEC_TO_MILLISEC = 1000;
24 static constexpr uint64_t MICROSEC_TO_MILLISEC = 1000;
25 static constexpr int64_t MILLI_TO_SEC = 1000;
26 static constexpr int64_t NANO_TO_SEC = 1000000000;
27 static constexpr int64_t NANO_TO_MILLI = NANO_TO_SEC / MILLI_TO_SEC;
28 
GetTimeMsByClockId(clockid_t clockId)29 static int64_t GetTimeMsByClockId(clockid_t clockId)
30 {
31     struct timespec tv = { 0 };
32     if (clock_gettime(clockId, &tv) < 0) {
33         return 0;
34     }
35     return tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
36 }
37 }
38 
GetCurrentTimeMicros(void)39 uint64_t PasteBoardTime::GetCurrentTimeMicros(void)
40 {
41     struct timeval tv = { 0, 0 };
42     gettimeofday(&tv, nullptr);
43     return (static_cast<uint64_t>(tv.tv_sec) * SEC_TO_MILLISEC +
44         static_cast<uint64_t>(tv.tv_usec) / MICROSEC_TO_MILLISEC);
45 }
46 
GetBootTimeMs(void)47 int64_t PasteBoardTime::GetBootTimeMs(void)
48 {
49     return GetTimeMsByClockId(CLOCK_BOOTTIME);
50 }
51 
GetWallTimeMs(void)52 int64_t PasteBoardTime::GetWallTimeMs(void)
53 {
54     return GetTimeMsByClockId(CLOCK_REALTIME);
55 }
56 } // namespace MiscServices
57 } // namespace OHOS
58