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 #include "time_util.h"
17 
18 #include <chrono>
19 #include <iomanip>
20 #include <sstream>
21 #include <sys/time.h>
22 #include <unistd.h>
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace TimeUtil {
27 namespace {
28 constexpr int8_t MILLISECONDS_LENGTH = 3;
29 }
StrToTimeStamp(const std::string & tmStr,const std::string & format)30 time_t StrToTimeStamp(const std::string& tmStr, const std::string& format)
31 {
32     std::string stTime = tmStr;
33     struct tm tmFormat { 0 };
34     strptime(stTime.c_str(), format.c_str(), &tmFormat);
35     tmFormat.tm_isdst = -1;
36     return mktime(&tmFormat);
37 }
38 
GenerateTimestamp()39 uint64_t GenerateTimestamp()
40 {
41     struct timeval now;
42     if (gettimeofday(&now, nullptr) == -1) {
43         return 0;
44     }
45     return (now.tv_sec * SEC_TO_MICROSEC + now.tv_usec);
46 }
47 
Sleep(unsigned int seconds)48 void Sleep(unsigned int seconds)
49 {
50     sleep(seconds);
51 }
52 
GetSeconds()53 int64_t GetSeconds()
54 {
55     auto now = std::chrono::system_clock::now();
56     auto secs = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
57     return secs.count();
58 }
59 
GetMilliseconds()60 uint64_t GetMilliseconds()
61 {
62     auto now = std::chrono::system_clock::now();
63     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
64     return millisecs.count();
65 }
66 
TimestampFormatToDate(time_t timeStamp,const std::string & format)67 std::string TimestampFormatToDate(time_t timeStamp, const std::string& format)
68 {
69     char date[MAX_TIME_BUFF] = {0};
70     struct tm result {};
71     if (localtime_r(&timeStamp, &result) != nullptr) {
72         strftime(date, MAX_TIME_BUFF, format.c_str(), &result);
73     }
74     return std::string(date);
75 }
76 
GetTimeZone()77 std::string GetTimeZone()
78 {
79     struct timeval currentTime;
80     if (gettimeofday(&currentTime, nullptr) != 0) {
81         return "";
82     }
83     time_t systemSeconds = currentTime.tv_sec;
84     struct tm tmLocal;
85     if (localtime_r(&systemSeconds, &tmLocal) == nullptr) {
86         return "";
87     }
88     int tzBufSize = 20;
89     char tz[tzBufSize];
90     auto ret = strftime(tz, tzBufSize, "%z", &tmLocal);
91     if (ret > 0) {
92         return std::string(tz);
93     }
94     return std::string("+0000");
95 }
96 
Get0ClockStampMs()97 int64_t Get0ClockStampMs()
98 {
99     time_t now = std::time(nullptr);
100     int64_t zero = now;
101     struct tm *l = std::localtime(&now);
102     if (l != nullptr) {
103         l->tm_hour = 0;
104         l->tm_min = 0;
105         l->tm_sec = 0;
106         zero = std::mktime(l) * SEC_TO_MILLISEC;  // time is 00:00:00
107     }
108     return zero > 0 ? zero : 0; // result of mktime is negative, when the date is 19700101
109 }
110 
GetSteadyClockTimeMs()111 uint64_t GetSteadyClockTimeMs()
112 {
113     auto now = std::chrono::steady_clock::now();
114     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
115     return millisecs.count();
116 }
117 
TimeCalculator(std::shared_ptr<uint64_t> & timePtr)118 TimeCalculator::TimeCalculator(std::shared_ptr<uint64_t>& timePtr)
119 {
120     this->time_ = timePtr;
121     this->startTime_ = GenerateTimestamp();
122     this->endTime_ = 0;
123 }
124 
~TimeCalculator()125 TimeCalculator::~TimeCalculator()
126 {
127     this->endTime_ = GenerateTimestamp();
128     if (this->time_ != nullptr && this->endTime_ > this->startTime_) {
129         *(this->time_) += this->endTime_ - this->startTime_;
130     }
131 }
132 
FormatTime(const int64_t timestamp,const std::string & format)133 std::string FormatTime(const int64_t timestamp, const std::string &format)
134 {
135     std::time_t tt = static_cast<std::time_t>(timestamp / SEC_TO_MILLISEC);
136     std::tm t = *std::localtime(&tt);
137     char buffer[MAX_BUFFER_SIZE] = {0};
138     std::strftime(buffer, sizeof(buffer), format.c_str(), &t);
139     return std::string(buffer);
140 }
141 
GetNanoTime()142 uint64_t GetNanoTime()
143 {
144     auto nanoNow = std::chrono::steady_clock::now().time_since_epoch();
145     return nanoNow.count();
146 }
147 
GetBootTimeMs()148 uint64_t GetBootTimeMs()
149 {
150     struct timespec ts;
151     if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0) {
152         return static_cast<uint64_t>(ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC);
153     }
154     return 0;
155 }
156 
GetFormattedTimestampEndWithMilli()157 std::string GetFormattedTimestampEndWithMilli()
158 {
159     auto milliSeconds = GetMilliseconds();
160     auto seconds = milliSeconds / SEC_TO_MILLISEC;
161     std::string formattedTimeStamp = TimestampFormatToDate(seconds, "%Y%m%d%H%M%S");
162     std::stringstream ss;
163     ss << formattedTimeStamp;
164     milliSeconds = milliSeconds % SEC_TO_MILLISEC;
165     ss << std::setfill('0') << std::setw(MILLISECONDS_LENGTH) << milliSeconds;
166     return ss.str();
167 }
168 } // namespace TimeUtil
169 } // namespace HiviewDFX
170 } // namespace OHOS
171