15490a39dSopenharmony_ci/* 25490a39dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 35490a39dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45490a39dSopenharmony_ci * you may not use this file except in compliance with the License. 55490a39dSopenharmony_ci * You may obtain a copy of the License at 65490a39dSopenharmony_ci * 75490a39dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85490a39dSopenharmony_ci * 95490a39dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105490a39dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115490a39dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125490a39dSopenharmony_ci * See the License for the specific language governing permissions and 135490a39dSopenharmony_ci * limitations under the License. 145490a39dSopenharmony_ci */ 155490a39dSopenharmony_ci#ifndef TIME_UTIL_H 165490a39dSopenharmony_ci#define TIME_UTIL_H 175490a39dSopenharmony_ci 185490a39dSopenharmony_ci#include <string> 195490a39dSopenharmony_ci#include <iostream> 205490a39dSopenharmony_ci#include "base_constants.h" 215490a39dSopenharmony_ci 225490a39dSopenharmony_cinamespace OHOS { 235490a39dSopenharmony_cinamespace IntellVoiceUtils { 245490a39dSopenharmony_cienum TimeFormat { 255490a39dSopenharmony_ci TIME_FORMAT_DEFAULT = 0, 265490a39dSopenharmony_ci TIME_FORMAT_CONTINOUS, 275490a39dSopenharmony_ci TIME_FORMAT_STANDARD, 285490a39dSopenharmony_ci TIME_FORMAT_NUM 295490a39dSopenharmony_ci}; 305490a39dSopenharmony_ci 315490a39dSopenharmony_ciclass AutoTimer { 325490a39dSopenharmony_cipublic: 335490a39dSopenharmony_ci AutoTimer(); 345490a39dSopenharmony_ci explicit AutoTimer(const std::string &logInfo); 355490a39dSopenharmony_ci virtual ~AutoTimer(); 365490a39dSopenharmony_ci 375490a39dSopenharmony_ci void PrintTimeElapse(); 385490a39dSopenharmony_ci void PrintTimeElapse(const std::string &logInfo); 395490a39dSopenharmony_ci void Reset(); 405490a39dSopenharmony_ci long TimeElapseUs(); 415490a39dSopenharmony_ci uint32_t TimeElapseMs(); 425490a39dSopenharmony_ci uint32_t TimeElapseS(); 435490a39dSopenharmony_ci 445490a39dSopenharmony_ciprivate: 455490a39dSopenharmony_ci std::string logInfo_; 465490a39dSopenharmony_ci timespec timeStart_ { 0, 0 }; 475490a39dSopenharmony_ci bool isReset_ { true }; 485490a39dSopenharmony_ci}; 495490a39dSopenharmony_ci 505490a39dSopenharmony_ciclass TimeUtil { 515490a39dSopenharmony_cipublic: 525490a39dSopenharmony_ci TimeUtil() {} 535490a39dSopenharmony_ci ~TimeUtil() {} 545490a39dSopenharmony_ci static std::string GetCurrTime(TimeFormat format = TIME_FORMAT_DEFAULT); 555490a39dSopenharmony_ci static std::string GetCurrTimeUs(); 565490a39dSopenharmony_ci static time_t GetFormatTimeToSec(const std::string &formatTime); 575490a39dSopenharmony_ci static bool IsFormatTimeExpired(const std::string &formatTime, int maxKeepTime); 585490a39dSopenharmony_ci static void GetTime(timespec &start); 595490a39dSopenharmony_ci static uint32_t TimeElapse(const timespec &start); 605490a39dSopenharmony_ci static void TimeElapse(const timespec &start, const timespec &end); 615490a39dSopenharmony_ci static long TimeElapseUs(const timespec &start, const timespec &end); 625490a39dSopenharmony_ci static uint64_t GetCurrentTimeMs(); 635490a39dSopenharmony_ci}; 645490a39dSopenharmony_ci 655490a39dSopenharmony_ciinline void TimeUtil::GetTime(timespec &start) 665490a39dSopenharmony_ci{ 675490a39dSopenharmony_ci if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) { 685490a39dSopenharmony_ci return; 695490a39dSopenharmony_ci } 705490a39dSopenharmony_ci} 715490a39dSopenharmony_ci 725490a39dSopenharmony_ciinline uint32_t TimeUtil::TimeElapse(const timespec &start) 735490a39dSopenharmony_ci{ 745490a39dSopenharmony_ci timespec current; 755490a39dSopenharmony_ci if (clock_gettime(CLOCK_REALTIME, ¤t) == -1) { 765490a39dSopenharmony_ci return 0; 775490a39dSopenharmony_ci } 785490a39dSopenharmony_ci 795490a39dSopenharmony_ci return (current.tv_sec > start.tv_sec) ? (current.tv_sec - start.tv_sec) : 0; 805490a39dSopenharmony_ci} 815490a39dSopenharmony_ci 825490a39dSopenharmony_ciinline void TimeUtil::TimeElapse(const timespec &start, const timespec &end) 835490a39dSopenharmony_ci{ 845490a39dSopenharmony_ci long secs = end.tv_sec - start.tv_sec; 855490a39dSopenharmony_ci long hour = secs / (H_PER_MIN * MIN_PER_S); 865490a39dSopenharmony_ci long min = (secs % (H_PER_MIN * MIN_PER_S)) / MIN_PER_S; 875490a39dSopenharmony_ci long sec = secs % MIN_PER_S; 885490a39dSopenharmony_ci 895490a39dSopenharmony_ci std::cout << hour << ":" << min << ":" << sec << std::endl; 905490a39dSopenharmony_ci return; 915490a39dSopenharmony_ci} 925490a39dSopenharmony_ci 935490a39dSopenharmony_ciinline long TimeUtil::TimeElapseUs(const timespec &start, const timespec &end) 945490a39dSopenharmony_ci{ 955490a39dSopenharmony_ci long usecs = MS_PER_US * S_PER_MS * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / US_PER_NS; 965490a39dSopenharmony_ci return usecs; 975490a39dSopenharmony_ci} 985490a39dSopenharmony_ci} 995490a39dSopenharmony_ci} 1005490a39dSopenharmony_ci 1015490a39dSopenharmony_ci#endif 102