1 /*
2 * Copyright (c) 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 "res_sched_time_util.h"
17
18 namespace OHOS {
19 namespace ResourceSchedule {
20 namespace ResCommonUtil {
21 namespace {
22 constexpr int32_t MAX_TIME_STR_LEN = 64;
23 constexpr int32_t SEC_TO_MILLISEC = 1000;
24 constexpr int32_t MILLISEC_LENGTH = 3;
25 constexpr int32_t MAX_FORMAT_BUFF = 32;
26 }
GetNowSecTime(bool steady)27 int64_t GetNowSecTime(bool steady)
28 {
29 std::chrono::seconds secs;
30 if (steady) {
31 // get steady time, it will not change whe system time change.
32 secs = std::chrono::duration_cast<std::chrono::seconds>(
33 std::chrono::steady_clock::now().time_since_epoch());
34 } else {
35 // get system time, it can change.
36 secs = std::chrono::duration_cast<std::chrono::seconds>(
37 std::chrono::system_clock::now().time_since_epoch());
38 }
39 return static_cast<int64_t>(secs.count());
40 }
41
GetNowMillTime(bool steady)42 int64_t GetNowMillTime(bool steady)
43 {
44 std::chrono::milliseconds millSecs;
45 if (steady) {
46 // get steady time, it will not change whe system time change.
47 millSecs = std::chrono::duration_cast<std::chrono::milliseconds>(
48 std::chrono::steady_clock::now().time_since_epoch());
49 } else {
50 // get system time, it can change.
51 millSecs = std::chrono::duration_cast<std::chrono::milliseconds>(
52 std::chrono::system_clock::now().time_since_epoch());
53 }
54 return static_cast<int64_t>(millSecs.count());
55 }
56
GetNowMicroTime(bool steady)57 int64_t GetNowMicroTime(bool steady)
58 {
59 std::chrono::microseconds microSecs;
60 if (steady) {
61 // get steady time, it will not change whe system time change.
62 microSecs = std::chrono::duration_cast<std::chrono::microseconds>(
63 std::chrono::steady_clock::now().time_since_epoch());
64 } else {
65 // get system time, it can change.
66 microSecs = std::chrono::duration_cast<std::chrono::microseconds>(
67 std::chrono::system_clock::now().time_since_epoch());
68 }
69 return static_cast<int64_t>(microSecs.count());
70 }
71
GetCurrentTimestamp()72 int64_t GetCurrentTimestamp()
73 {
74 // get current time,precision is ms + us
75 struct timeval currentTime;
76 gettimeofday(¤tTime, nullptr);
77 return static_cast<int64_t>(currentTime.tv_sec) * SEC_TO_MILLISEC + currentTime.tv_usec /SEC_TO_MILLISEC;
78 }
79
ConvertTimestampToStr(int64_t timestamp)80 std::string ConvertTimestampToStr(int64_t timestamp)
81 {
82 char timeStr[MAX_TIME_STR_LEN];
83 //timestamp is in millsecond unit, divide 1000 to second
84 auto t = static_cast<std::time_t>(timestamp / SEC_TO_MILLISEC);
85 auto local = std::localtime(&t);
86 if (!local) {
87 return "";
88 }
89 std::strftime(timeStr, MAX_TIME_STR_LEN, "%Y-%m-%d %H-%M-%S", local);
90 std::stringstream oss;
91 //milliseconds in timeStr should be 3 characters length
92 oss << timeStr << "." << std::setw(MILLISEC_LENGTH) << std::setfill('0') << (timestamp % SEC_TO_MILLISEC);
93 return oss.str();
94 }
95
GetNextMillTimeStamp(const int64_t time,bool steady)96 int64_t GetNextMillTimeStamp(const int64_t time, bool steady)
97 {
98 return GetNowMillTime(steady) + time;
99 }
100
FormatSeconds(const int64_t seconds,const std::string & format)101 std::string FormatSeconds(const int64_t seconds, const std::string& format)
102 {
103 struct tm* time = std::localtime(&seconds);
104 if (time == nullptr) {
105 return "";
106 }
107
108 char result[MAX_FORMAT_BUFF] = {0};
109 if (strftime(result, MAX_FORMAT_BUFF, format.c_str(), time) == 0) {
110 return "";
111 }
112 return std::string(result);
113 }
114
FormatTimeWithMillis(const int64_t millis)115 std::string FormatTimeWithMillis(const int64_t millis)
116 {
117 if (millis <= 0) {
118 return "";
119 }
120 auto tp = std::chrono::system_clock::from_time_t(0) + std::chrono::milliseconds(millis);
121 std::ostringstream oss;
122 auto time_t = std::chrono::system_clock::to_time_t(tp);
123 auto tm = std::localtime(&time_t);
124 if (tm == nullptr) {
125 return "";
126 }
127 oss << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
128 auto duration = tp.time_since_epoch();
129 auto subsec = std::chrono::duration_cast<std::chrono::milliseconds>(duration) % std::chrono::seconds(1);
130 oss << "." << std::setfill('0') << std::setw(MILLISEC_LENGTH) << (subsec.count() / SEC_TO_MILLISEC);
131 return oss.str();
132 }
133 }
134 } // namespace ResourceSchedule
135 } // namespace OHOS
136