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