• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
46     std::string mLogInfo;
47     timespec mTimeStart { 0, 0 };
48     bool mIsReset { true };
49 };
50 
51 class TimeUtil {
52 public:
TimeUtil()53     TimeUtil() {}
~TimeUtil()54     ~TimeUtil() {}
55     static std::string GetCurrTime(TimeFormat format = TIME_FORMAT_DEFAULT);
56     static std::string GetCurrTimeUs();
57     static time_t GetFormatTimeToSec(const std::string &formatTime);
58     static bool IsFormatTimeExpired(const std::string &formatTime, int maxKeepTime);
59     static void GetTime(timespec &start);
60     static uint32_t TimeElapse(const timespec &start);
61     static void TimeElapse(const timespec &start, const timespec &end);
62     static long TimeElapseUs(const timespec &start, const timespec &end);
63     static uint64_t GetCurrentTimeMs();
64 };
65 
GetTime(timespec & start)66 inline void TimeUtil::GetTime(timespec &start)
67 {
68     if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
69         return;
70     }
71 }
72 
TimeElapse(const timespec & start)73 inline uint32_t TimeUtil::TimeElapse(const timespec &start)
74 {
75     timespec current;
76     if (clock_gettime(CLOCK_REALTIME, &current) == -1) {
77         return 0;
78     }
79 
80     return (current.tv_sec > start.tv_sec) ? (current.tv_sec - start.tv_sec) : 0;
81 }
82 
TimeElapse(const timespec & start,const timespec & end)83 inline void TimeUtil::TimeElapse(const timespec &start, const timespec &end)
84 {
85     long secs = end.tv_sec - start.tv_sec;
86     long hour = secs / (H_PER_MIN * MIN_PER_S);
87     long min = (secs % (H_PER_MIN * MIN_PER_S)) / MIN_PER_S;
88     long sec = secs % MIN_PER_S;
89 
90     std::cout << hour << ":" << min << ":" << sec << std::endl;
91     return;
92 }
93 
TimeElapseUs(const timespec & start,const timespec & end)94 inline long TimeUtil::TimeElapseUs(const timespec &start, const timespec &end)
95 {
96     long usecs = MS_PER_US * S_PER_MS * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / US_PER_NS;
97     return usecs;
98 }
99 }
100 }
101 
102 #endif
103