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 "base/utils/time_util.h"
17
18 #include <chrono>
19 #include <ctime>
20 #include <iomanip>
21 #include <sstream>
22 #include <sys/time.h>
23
24 #include "base/log/log.h"
25 #include "base/utils/utils.h"
26
27 namespace OHOS::Ace {
28 namespace {
29
30 constexpr int64_t SEC_TO_MICROSEC = 1000000;
31 constexpr int64_t SEC_TO_NANOSEC = 1000000000;
32 constexpr int64_t MICROSEC_TO_NANOSEC = 1000;
33 constexpr int64_t SEC_TO_MILLISEC = 1000;
34 constexpr int64_t MILLISEC_TO_MICROSEC = 1000;
35 constexpr int32_t HOURS_WEST_GEOGRAPHICAL_LOWER_LIMIT = -12;
36 constexpr int32_t HOURS_WEST_LOWER_LIMIT = -14;
37 constexpr int32_t HOURS_WEST_UPPER_LIMIT = 12;
38 constexpr int32_t TOTAL_SECONDS_OF_DAY = 24 * 60 * 60;
39 constexpr int32_t TOTAL_SECONDS_OF_HOUR = 60 * 60;
40 constexpr int32_t TOTAL_SECONDS_OF_MINUTE = 60;
41 constexpr int32_t TOTAL_MINUTE_OF_HOUR = 60;
42 constexpr int32_t TWENTY_FOUR_HOUR_BASE = 24;
43 constexpr int32_t TWELVE_HOUR_BASE = 12;
44 constexpr int32_t DAY_TIME_LOWER_LIMIT = 6;
45 constexpr int32_t DAY_TIME_UPPER_LIMIT = 18;
46 constexpr int32_t MAX_TIME_STR_LEN = 64;
47 } // namespace
48
GetMicroTickCount()49 int64_t GetMicroTickCount()
50 {
51 struct timespec ts;
52 clock_gettime(CLOCK_MONOTONIC, &ts);
53 return (ts.tv_sec * SEC_TO_MICROSEC + ts.tv_nsec / MICROSEC_TO_NANOSEC);
54 }
55
GetSysTimestamp()56 int64_t GetSysTimestamp()
57 {
58 struct timespec ts;
59 clock_gettime(CLOCK_MONOTONIC, &ts);
60 return ts.tv_sec * SEC_TO_NANOSEC + ts.tv_nsec;
61 }
62
GetCurrentTimestamp()63 int64_t GetCurrentTimestamp()
64 {
65 struct timeval currentTime;
66 gettimeofday(¤tTime, nullptr);
67 return static_cast<int64_t>(currentTime.tv_sec) * SEC_TO_MILLISEC + currentTime.tv_usec / MILLISEC_TO_MICROSEC;
68 }
69
ConvertTimestampToStr(int64_t timestamp)70 std::string ConvertTimestampToStr(int64_t timestamp)
71 {
72 char timeStr[MAX_TIME_STR_LEN];
73 // timestamp is in millisecond unit, divide 1000 to second
74 auto t = static_cast<std::time_t>(timestamp / SEC_TO_MILLISEC);
75 auto local = std::localtime(&t);
76 std::strftime(timeStr, MAX_TIME_STR_LEN, "%Y-%m-%d %H:%M:%S", local);
77 std::stringstream oss;
78 // milliseconds in timestr should be 3 characters length
79 oss << timeStr << "." << std::setw(3) << std::setfill('0') << (timestamp % SEC_TO_MILLISEC);
80 return oss.str();
81 }
82
GetTimeOfNow(int32_t hoursWest)83 TimeOfNow GetTimeOfNow(int32_t hoursWest)
84 {
85 struct timeval currentTime;
86 struct timezone timeZone;
87 gettimeofday(¤tTime, &timeZone);
88
89 TimeOfNow timeOfNow(hoursWest);
90 int32_t minutesWest = timeZone.tz_minuteswest;
91 if (IsHoursWestValid(timeOfNow.hoursWest_)) {
92 minutesWest = Round(TOTAL_MINUTE_OF_HOUR * timeOfNow.hoursWest_);
93 } else {
94 // when [hoursWest] is invalid, set current time zone to [hoursWest].
95 if (!NearEqual(timeOfNow.hoursWest_, INT_MAX)) { // default value of hoursWest_ is INT_MAX
96 LOGW("hoursWest [%{public}d] is invalid, use current time zone.", timeOfNow.hoursWest_);
97 }
98 timeOfNow.hoursWest_ = timeZone.tz_minuteswest / TOTAL_MINUTE_OF_HOUR;
99 }
100 int secondsOfToday = currentTime.tv_sec % TOTAL_SECONDS_OF_DAY - minutesWest * TOTAL_SECONDS_OF_MINUTE;
101 if (secondsOfToday < 0) {
102 secondsOfToday += TOTAL_SECONDS_OF_DAY;
103 }
104 timeOfNow.minute_ = (secondsOfToday / TOTAL_SECONDS_OF_MINUTE) % TOTAL_MINUTE_OF_HOUR +
105 secondsOfToday % TOTAL_SECONDS_OF_MINUTE / TOTAL_SECONDS_OF_MINUTE;
106 timeOfNow.hour24_ =
107 (secondsOfToday / TOTAL_SECONDS_OF_HOUR) % TWENTY_FOUR_HOUR_BASE + timeOfNow.minute_ / TOTAL_MINUTE_OF_HOUR;
108 timeOfNow.hour12_ =
109 (timeOfNow.hour24_ < TWELVE_HOUR_BASE) ? timeOfNow.hour24_ : (timeOfNow.hour24_ - TWELVE_HOUR_BASE);
110 timeOfNow.second_ = secondsOfToday % TOTAL_SECONDS_OF_MINUTE;
111 timeOfNow.timeUsec_ = currentTime.tv_usec;
112 return timeOfNow;
113 }
114
IsHoursWestValid(int32_t & hoursWest)115 bool IsHoursWestValid(int32_t& hoursWest)
116 {
117 // valid hoursWest is within [-14, 12]
118 bool isValid = GreatOrEqual(hoursWest, HOURS_WEST_LOWER_LIMIT) && LessOrEqual(hoursWest, HOURS_WEST_UPPER_LIMIT);
119 if (!isValid) {
120 return false;
121 }
122 // Theoretically, the time zone range should be [-12, +12], but some countries and regions that cross the
123 // International Date Line use -13(UTC+13) and -14(UTC+14) to keep the whole country or region at the same date.
124 bool isSpecialTimeZone = LessNotEqual(hoursWest, HOURS_WEST_GEOGRAPHICAL_LOWER_LIMIT);
125 if (isSpecialTimeZone) {
126 hoursWest += TWENTY_FOUR_HOUR_BASE;
127 }
128 return true;
129 }
130
IsDayTime(const TimeOfNow & timeOfNow)131 bool IsDayTime(const TimeOfNow& timeOfNow)
132 {
133 return GreatOrEqual(timeOfNow.hour24_, DAY_TIME_LOWER_LIMIT) &&
134 LessNotEqual(timeOfNow.hour24_, DAY_TIME_UPPER_LIMIT);
135 }
136
GetTimeOfZone(int32_t hoursWest)137 TimeOfZone GetTimeOfZone(int32_t hoursWest)
138 {
139 struct timeval currentTime;
140 struct timezone timeZone;
141 gettimeofday(¤tTime, &timeZone);
142
143 TimeOfZone timeOfZone(hoursWest);
144 int32_t minutesWest = timeZone.tz_minuteswest;
145 if (HoursWestIsValid(timeOfZone.hoursWest_)) {
146 minutesWest = Round(TOTAL_MINUTE_OF_HOUR * timeOfZone.hoursWest_);
147 } else {
148 // when [hoursWest] is invalid, set current time zone to [hoursWest].
149 if (!NearEqual(timeOfZone.hoursWest_, DEFAULT_HOURS_WEST)) { // default value of hoursWest_ is DEFAULT_HOURS_WEST
150 LOGW("hoursWest [%{public}d] is invalid, use current time zone.", timeOfZone.hoursWest_);
151 }
152 timeOfZone.hoursWest_ = minutesWest / TOTAL_MINUTE_OF_HOUR;
153 }
154 int secondsOfToday = currentTime.tv_sec % TOTAL_SECONDS_OF_DAY - minutesWest * TOTAL_SECONDS_OF_MINUTE;
155 if (secondsOfToday < 0) {
156 secondsOfToday += TOTAL_SECONDS_OF_DAY;
157 }
158 timeOfZone.minute_ = (secondsOfToday / TOTAL_SECONDS_OF_MINUTE) % TOTAL_MINUTE_OF_HOUR +
159 secondsOfToday % TOTAL_SECONDS_OF_MINUTE / TOTAL_SECONDS_OF_MINUTE;
160 timeOfZone.hour24_ =
161 (secondsOfToday / TOTAL_SECONDS_OF_HOUR) % TWENTY_FOUR_HOUR_BASE + timeOfZone.minute_ / TOTAL_MINUTE_OF_HOUR;
162 timeOfZone.hour12_ =
163 (timeOfZone.hour24_ < TWELVE_HOUR_BASE) ? timeOfZone.hour24_ : (timeOfZone.hour24_ - TWELVE_HOUR_BASE);
164 timeOfZone.second_ = secondsOfToday % TOTAL_SECONDS_OF_MINUTE;
165 timeOfZone.timeUsec_ = currentTime.tv_usec;
166 return timeOfZone;
167 }
168
HoursWestIsValid(int32_t & hoursWest)169 bool HoursWestIsValid(int32_t& hoursWest)
170 {
171 // valid hoursWest is within [-14, 12]
172 bool isValid = GreatOrEqual(hoursWest, HOURS_WEST_LOWER_LIMIT) && LessOrEqual(hoursWest, HOURS_WEST_UPPER_LIMIT);
173 if (!isValid) {
174 return false;
175 }
176 // Theoretically, the time zone range should be [-12, +12], but some countries and regions that cross the
177 // International Date Line use -13(UTC+13) and -14(UTC+14) to keep the whole country or region at the same date.
178 bool isSpecialTimeZone = LessNotEqual(hoursWest, HOURS_WEST_GEOGRAPHICAL_LOWER_LIMIT);
179 if (isSpecialTimeZone) {
180 hoursWest += TWENTY_FOUR_HOUR_BASE;
181 }
182 return true;
183 }
184
IsDayTime(const TimeOfZone & timeOfZone)185 bool IsDayTime(const TimeOfZone& timeOfZone)
186 {
187 return GreatOrEqual(timeOfZone.hour24_, DAY_TIME_LOWER_LIMIT) &&
188 LessNotEqual(timeOfZone.hour24_, DAY_TIME_UPPER_LIMIT);
189 }
190
191 } // namespace OHOS::Ace
192