1 /*
2 * Copyright (c) 2021 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 #include <chrono>
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace TimeUtil {
StrToTimeStamp(const std::string & tmStr,const std::string & format)24 time_t StrToTimeStamp(const std::string& tmStr, const std::string& format)
25 {
26 std::string stTime = tmStr;
27 struct tm tmFormat { 0 };
28 strptime(stTime.c_str(), format.c_str(), &tmFormat);
29 tmFormat.tm_isdst = -1;
30 return mktime(&tmFormat);
31 }
32
GenerateTimestamp()33 uint64_t GenerateTimestamp()
34 {
35 struct timeval now { 0 };
36 if (gettimeofday(&now, nullptr) == -1) {
37 return 0;
38 }
39 return (now.tv_sec * SEC_TO_MICROSEC + now.tv_usec);
40 }
41
Sleep(unsigned int seconds)42 void Sleep(unsigned int seconds)
43 {
44 sleep(seconds);
45 }
46
GetMillSecOfSec()47 int GetMillSecOfSec()
48 {
49 auto now = std::chrono::system_clock::now();
50 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
51 return millisecs.count() % SEC_TO_MILLISEC;
52 }
53
GetMilliseconds()54 uint64_t GetMilliseconds()
55 {
56 auto now = std::chrono::system_clock::now();
57 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
58 return millisecs.count();
59 }
60
TimestampFormatToDate(time_t timeStamp,const std::string & format)61 std::string TimestampFormatToDate(time_t timeStamp, const std::string& format)
62 {
63 char date[MAX_TIME_BUFF] = {0};
64 struct tm result {};
65 if (localtime_r(&timeStamp, &result) != nullptr) {
66 strftime(date, MAX_TIME_BUFF, format.c_str(), &result);
67 }
68 return std::string(date);
69 }
70
GetTimeZone()71 std::string GetTimeZone()
72 {
73 struct timeval currentTime;
74 if (gettimeofday(¤tTime, nullptr) != 0) {
75 return "";
76 }
77 time_t systemSeconds = currentTime.tv_sec;
78 struct tm tmLocal;
79 if (localtime_r(&systemSeconds, &tmLocal) == nullptr) {
80 return "";
81 }
82 int tzBufSize = 20;
83 char tz[tzBufSize];
84 auto ret = strftime(tz, tzBufSize, "%z", &tmLocal);
85 if (ret > 0) {
86 return std::string(tz);
87 }
88 return std::string("+0000");
89 }
90
Get0ClockStampMs()91 int64_t Get0ClockStampMs()
92 {
93 time_t now = std::time(nullptr);
94 int64_t zero = now;
95 struct tm *l = std::localtime(&now);
96 if (l != nullptr) {
97 l->tm_hour = 0;
98 l->tm_min = 0;
99 l->tm_sec = 0;
100 zero = std::mktime(l) * SEC_TO_MILLISEC; // time is 00:00:00
101 }
102 return zero;
103 }
104 } // namespace TimeUtil
105 } // namespace HiviewDFX
106 } // namespace OHOS
107