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