• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H
18 
19 #include <cstdint>
20 #include <ctime>
21 #include <float.h>
22 #include <limits.h>
23 
24 #include "base/utils/macros.h"
25 
26 namespace OHOS::Ace {
27 constexpr int32_t DEFAULT_HOURS_WEST = -8;
28 
29 /**
30 * The GetMicroTickCount function get current microseconds since the system was started.
31 */
32 ACE_EXPORT int64_t GetMicroTickCount();
33 
34 int64_t GetSysTimestamp();
35 
36 struct TimeOfNow final {
hoursWest_final37     explicit TimeOfNow(double hoursWest = INT_MAX) : hoursWest_(hoursWest) {}
38     ~TimeOfNow() = default;
39 
40     // hours west of Greenwich, for e.g., [hoursWest] is [-8] in  UTC+8.
41     // Valid range of [hoursWest] is [-14, 12]. Set default value to DBL_MAX to use current time zone by default.
42     int32_t hoursWest_ = INT_MAX;
43     int32_t second_ = 0;
44     int32_t minute_ = 0;
45     int32_t hour12_ = 0; // 12-hour clock
46     int32_t hour24_ = 0; // 24-hour clock
47     int64_t timeUsec_ = 0L;   // microsecond. 1 second = 1000 millisecond = 1000000 microsecond
48 };
49 
50 bool IsHoursWestValid(int32_t& hoursWest);
51 
52 TimeOfNow GetTimeOfNow(int32_t hoursWest = INT_MAX);
53 
54 bool IsDayTime(const TimeOfNow& timeOfNow);
55 
56 struct TimeOfZone final {
hoursWest_final57     explicit TimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST) : hoursWest_(hoursWest) {}
58     ~TimeOfZone() = default;
59 
60     // hours west of Greenwich, for e.g., [hoursWest] is [-8] in  UTC+8.
61     // Valid range of [hoursWest] is [-14, 12]. Set default value to DEFAULT_HOURS_WEST to use current time zone by default.
62     int32_t hoursWest_ = DEFAULT_HOURS_WEST;
63     int32_t second_ = 0;
64     int32_t minute_ = 0;
65     int32_t hour12_ = 0; // 12-hour clock
66     int32_t hour24_ = 0; // 24-hour clock
67     int64_t timeUsec_ = 0L;   // microsecond. 1 second = 1000 millisecond = 1000000 microsecond
68 };
69 
70 bool HoursWestIsValid(int32_t& hoursWest);
71 
72 TimeOfZone GetTimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST);
73 
74 bool IsDayTime(const TimeOfZone& timeOfZone);
75 
76 } // namespace OHOS::Ace
77 
78 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H
79