1 /*
2  * Copyright (c) 2021-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 #ifndef UTILITY_TIME_UTIL_H
17 #define UTILITY_TIME_UTIL_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace TimeUtil {
26 constexpr int64_t SEC_TO_NANOSEC = 1000000000;
27 constexpr int64_t SEC_TO_MICROSEC = 1000000;
28 constexpr int64_t SEC_TO_MILLISEC = 1000;
29 constexpr int64_t MILLISEC_TO_NANOSEC = 1000000;
30 constexpr int64_t MICROSEC_TO_NANOSEC = 1000;
31 constexpr int SECONDS_PER_MINUTE = 60; // 60 seconds
32 constexpr int SECONDS_PER_HOUR = 3600; // 60 * 60
33 constexpr int SECONDS_PER_DAY = 86400; // 60 * 60 * 24
34 constexpr uint64_t MILLISECS_PER_DAY = 60 * 60 * 24 * 1000;
35 constexpr int MAX_TIME_BUFF = 64; // 64 : for example 2021-05-27-01-01-01
36 constexpr int MAX_BUFFER_SIZE = 80;
37 class TimeCalculator {
38 public:
39     TimeCalculator(std::shared_ptr<uint64_t>& timePtr);
40     ~TimeCalculator();
41 
42 private:
43     std::shared_ptr<uint64_t> time_;
44     uint64_t startTime_;
45     uint64_t endTime_;
46 };
47 
48 uint64_t GetNanoTime();
49 uint64_t GetTimeOfDay();
50 uint64_t GenerateTimestamp();
51 uint64_t GetMilliseconds();
52 time_t StrToTimeStamp(const std::string &tmStr, const std::string& format);
53 void Sleep(unsigned int seconds);
54 int64_t GetSeconds();
55 std::string TimestampFormatToDate(time_t timeStamp, const std::string& format);
56 std::string GetTimeZone();
57 int64_t Get0ClockStampMs();
58 uint64_t GetSteadyClockTimeMs();
59 uint64_t GetBootTimeMs();
60 std::string FormatTime(const int64_t timestamp, const std::string &format);
61 
62 // time format as YYYYmmddHHMMSSxxx
63 // eg. 202404222319765
64 std::string GetFormattedTimestampEndWithMilli();
65 } // namespace TimeUtil
66 } // namespace HiviewDFX
67 } // namespace OHOS
68 #endif // UTILITY_TIME_UTIL_H