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 #include "time_util.h"
17
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include <chrono>
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace TimeUtil {
StrToTimeStamp(const std::string & tmStr,const std::string & format)25 time_t StrToTimeStamp(const std::string& tmStr, const std::string& format)
26 {
27 std::string stTime = tmStr;
28 struct tm tmFormat { 0 };
29 strptime(stTime.c_str(), format.c_str(), &tmFormat);
30 tmFormat.tm_isdst = -1;
31 return mktime(&tmFormat);
32 }
33
GenerateTimestamp()34 uint64_t GenerateTimestamp()
35 {
36 struct timeval now;
37 if (gettimeofday(&now, nullptr) == -1) {
38 return 0;
39 }
40 return (now.tv_sec * SEC_TO_MICROSEC + now.tv_usec);
41 }
42
Sleep(unsigned int seconds)43 void Sleep(unsigned int seconds)
44 {
45 sleep(seconds);
46 }
47
GetMillSecOfSec()48 int GetMillSecOfSec()
49 {
50 auto now = std::chrono::system_clock::now();
51 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
52 return millisecs.count() % SEC_TO_MILLISEC;
53 }
54
GetMilliseconds()55 uint64_t GetMilliseconds()
56 {
57 auto now = std::chrono::system_clock::now();
58 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
59 return millisecs.count();
60 }
61
TimestampFormatToDate(time_t timeStamp,const std::string & format)62 std::string TimestampFormatToDate(time_t timeStamp, const std::string& format)
63 {
64 char date[MAX_TIME_BUFF] = {0};
65 struct tm result {};
66 if (localtime_r(&timeStamp, &result) != nullptr) {
67 strftime(date, MAX_TIME_BUFF, format.c_str(), &result);
68 }
69 return std::string(date);
70 }
71
GetTimeZone()72 std::string GetTimeZone()
73 {
74 struct timeval currentTime;
75 if (gettimeofday(¤tTime, nullptr) != 0) {
76 return "";
77 }
78 time_t systemSeconds = currentTime.tv_sec;
79 struct tm tmLocal;
80 if (localtime_r(&systemSeconds, &tmLocal) == nullptr) {
81 return "";
82 }
83 int tzBufSize = 20;
84 char tz[tzBufSize];
85 auto ret = strftime(tz, tzBufSize, "%z", &tmLocal);
86 if (ret > 0) {
87 return std::string(tz);
88 }
89 return std::string("+0000");
90 }
91
Get0ClockStampMs()92 int64_t Get0ClockStampMs()
93 {
94 time_t now = std::time(nullptr);
95 int64_t zero = now;
96 struct tm *l = std::localtime(&now);
97 if (l != nullptr) {
98 l->tm_hour = 0;
99 l->tm_min = 0;
100 l->tm_sec = 0;
101 zero = std::mktime(l) * SEC_TO_MILLISEC; // time is 00:00:00
102 }
103 return zero;
104 }
105
GetSteadyClockTimeMs()106 uint64_t GetSteadyClockTimeMs()
107 {
108 auto now = std::chrono::steady_clock::now();
109 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
110 return millisecs.count();
111 }
112
TimeCalculator(std::shared_ptr<uint64_t> & timePtr)113 TimeCalculator::TimeCalculator(std::shared_ptr<uint64_t>& timePtr)
114 {
115 this->time_ = timePtr;
116 this->startTime_ = GenerateTimestamp();
117 this->endTime_ = 0;
118 }
119
~TimeCalculator()120 TimeCalculator::~TimeCalculator()
121 {
122 this->endTime_ = GenerateTimestamp();
123 if (this->time_ != nullptr && this->endTime_ > this->startTime_) {
124 *(this->time_) += this->endTime_ - this->startTime_;
125 }
126 }
127
FormatTime(const int64_t timestamp,const std::string & format)128 std::string FormatTime(const int64_t timestamp, const std::string &format)
129 {
130 std::time_t tt = static_cast<std::time_t>(timestamp / SEC_TO_MILLISEC);
131 std::tm t = *std::localtime(&tt);
132 char buffer[MAX_BUFFER_SIZE] = {0};
133 std::strftime(buffer, sizeof(buffer), format.c_str(), &t);
134 return std::string(buffer);
135 }
136
GetNanoTime()137 uint64_t GetNanoTime()
138 {
139 auto nanoNow = std::chrono::steady_clock::now().time_since_epoch();
140 return nanoNow.count();
141 }
142 } // namespace TimeUtil
143 } // namespace HiviewDFX
144 } // namespace OHOS
145