1 /* 2 * Copyright (C) 2021 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 "hc_time.h" 17 #include <stdio.h> 18 #include <time.h> 19 #include "hc_log.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 HcGetCurTime(void)25int64_t HcGetCurTime(void) 26 { 27 struct timespec start; 28 int res = clock_gettime(CLOCK_MONOTONIC, &start); 29 if (res != 0) { 30 LOGE("[TIMER]: clock_gettime fail. [Res] :%d", res); 31 return -1; 32 } 33 return start.tv_sec; 34 } 35 HcGetIntervalTime(int64_t startTime)36int64_t HcGetIntervalTime(int64_t startTime) 37 { 38 if (startTime < 0) { 39 LOGE("Start time is invalid"); 40 return -1; 41 } 42 struct timespec end; 43 int res = clock_gettime(CLOCK_MONOTONIC, &end); 44 if (res != 0) { 45 LOGE("[TIMER]: clock_gettime fail. [Res] :%d", res); 46 return -1; 47 } 48 if (end.tv_sec < startTime) { 49 LOGE("End time is invalid"); 50 return -1; 51 } 52 return (end.tv_sec - startTime); 53 } 54 55 #ifdef __cplusplus 56 } 57 #endif 58