1 /*
2  * Copyright (c) 2022-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 "adaptor_time.h"
17 
18 #include <time.h>
19 #include <sys/time.h>
20 #include "adaptor_log.h"
21 
22 #define MS_OF_S 1000
23 #define NS_OF_MS 1000000
24 
GetRtcTime(void)25 uint64_t GetRtcTime(void)
26 {
27     struct timespec curTime;
28     int res = clock_gettime(CLOCK_MONOTONIC, &curTime);
29     if (res != 0) {
30         LOG_ERROR("get time failed");
31         return 0;
32     }
33     return curTime.tv_sec * MS_OF_S + curTime.tv_nsec / NS_OF_MS;
34 }
35 
GetSystemTime(void)36 uint64_t GetSystemTime(void)
37 {
38     struct timespec curTime;
39     int res = clock_gettime(CLOCK_MONOTONIC, &curTime);
40     if (res != 0) {
41         LOG_ERROR("get time failed");
42         return 0;
43     }
44     return curTime.tv_sec * MS_OF_S + curTime.tv_nsec / NS_OF_MS;
45 }
46 
GetReeTime(void)47 uint64_t GetReeTime(void)
48 {
49     struct timeval curTime;
50     int res = gettimeofday(&curTime, NULL);
51     if (res != 0) {
52         LOG_ERROR("get time failed");
53         return 0;
54     }
55     return curTime.tv_sec * MS_OF_S + curTime.tv_usec / MS_OF_S;
56 }