1 /*
2 * Copyright (c) 2023 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 #ifndef TIME_UTIL_H
16 #define TIME_UTIL_H
17
18 #include <string>
19 #include <iostream>
20 #include "base_constants.h"
21
22 namespace OHOS {
23 namespace IntellVoiceUtils {
24 enum TimeFormat {
25 TIME_FORMAT_DEFAULT = 0,
26 TIME_FORMAT_CONTINOUS,
27 TIME_FORMAT_STANDARD,
28 TIME_FORMAT_NUM
29 };
30
31 class AutoTimer {
32 public:
33 AutoTimer();
34 explicit AutoTimer(const std::string &logInfo);
35 virtual ~AutoTimer();
36
37 void PrintTimeElapse();
38 void PrintTimeElapse(const std::string &logInfo);
39 void Reset();
40 long TimeElapseUs();
41 uint32_t TimeElapseMs();
42 uint32_t TimeElapseS();
43
44 private:
45 std::string logInfo_;
46 timespec timeStart_ { 0, 0 };
47 bool isReset_ { true };
48 };
49
50 class TimeUtil {
51 public:
TimeUtil()52 TimeUtil() {}
~TimeUtil()53 ~TimeUtil() {}
54 static std::string GetCurrTime(TimeFormat format = TIME_FORMAT_DEFAULT);
55 static std::string GetCurrTimeUs();
56 static time_t GetFormatTimeToSec(const std::string &formatTime);
57 static bool IsFormatTimeExpired(const std::string &formatTime, int maxKeepTime);
58 static void GetTime(timespec &start);
59 static uint32_t TimeElapse(const timespec &start);
60 static void TimeElapse(const timespec &start, const timespec &end);
61 static long TimeElapseUs(const timespec &start, const timespec &end);
62 static uint64_t GetCurrentTimeMs();
63 };
64
GetTime(timespec & start)65 inline void TimeUtil::GetTime(timespec &start)
66 {
67 if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
68 return;
69 }
70 }
71
TimeElapse(const timespec & start)72 inline uint32_t TimeUtil::TimeElapse(const timespec &start)
73 {
74 timespec current;
75 if (clock_gettime(CLOCK_REALTIME, ¤t) == -1) {
76 return 0;
77 }
78
79 return (current.tv_sec > start.tv_sec) ? (current.tv_sec - start.tv_sec) : 0;
80 }
81
TimeElapse(const timespec & start,const timespec & end)82 inline void TimeUtil::TimeElapse(const timespec &start, const timespec &end)
83 {
84 long secs = end.tv_sec - start.tv_sec;
85 long hour = secs / (H_PER_MIN * MIN_PER_S);
86 long min = (secs % (H_PER_MIN * MIN_PER_S)) / MIN_PER_S;
87 long sec = secs % MIN_PER_S;
88
89 std::cout << hour << ":" << min << ":" << sec << std::endl;
90 return;
91 }
92
TimeElapseUs(const timespec & start,const timespec & end)93 inline long TimeUtil::TimeElapseUs(const timespec &start, const timespec &end)
94 {
95 long usecs = MS_PER_US * S_PER_MS * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / US_PER_NS;
96 return usecs;
97 }
98 }
99 }
100
101 #endif
102