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 #include <string> 24 25 #include "base/utils/macros.h" 26 27 namespace OHOS::Ace { 28 constexpr int32_t DEFAULT_HOURS_WEST = -8; 29 30 /** 31 * The GetMicroTickCount function get current microseconds since the system was started. 32 */ 33 ACE_FORCE_EXPORT int64_t GetMicroTickCount(); 34 35 int64_t GetSysTimestamp(); 36 37 /** 38 * return milliseconds to 1970-1-1 0:0:0 39 */ 40 int64_t GetCurrentTimestamp(); 41 int64_t GetCurrentTimestampMicroSecond(); 42 std::string ConvertTimestampToStr(int64_t timestamp); 43 44 struct TimeOfNow final { 45 explicit TimeOfNow(double hoursWest = INT_MAX) : hoursWest_(hoursWest) {} 46 ~TimeOfNow() = default; 47 48 // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 49 // Valid range of [hoursWest] is [-14, 12]. Set default value to DBL_MAX to use current time zone by default. 50 int32_t hoursWest_ = INT_MAX; 51 int32_t second_ = 0; 52 int32_t minute_ = 0; 53 int32_t hour12_ = 0; // 12-hour clock 54 int32_t hour24_ = 0; // 24-hour clock 55 int64_t timeUsec_ = 0L; // microsecond. 1 second = 1000 millisecond = 1000000 microsecond 56 }; 57 58 bool IsHoursWestValid(int32_t& hoursWest); 59 60 TimeOfNow GetTimeOfNow(int32_t hoursWest = INT_MAX); 61 62 bool IsDayTime(const TimeOfNow& timeOfNow); 63 64 struct TimeOfZone final { 65 explicit TimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST) : hoursWest_(hoursWest) {} 66 ~TimeOfZone() = default; 67 68 // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 69 // Valid range of [hoursWest] is [-14, 12]. 70 // Set default value to DEFAULT_HOURS_WEST to use current time zone by default. 71 int32_t hoursWest_ = DEFAULT_HOURS_WEST; 72 int32_t second_ = 0; 73 int32_t minute_ = 0; 74 int32_t hour12_ = 0; // 12-hour clock 75 int32_t hour24_ = 0; // 24-hour clock 76 int64_t timeUsec_ = 0L; // microsecond. 1 second = 1000 millisecond = 1000000 microsecond 77 }; 78 79 bool HoursWestIsValid(int32_t& hoursWest); 80 81 TimeOfZone GetTimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST); 82 83 bool IsDayTime(const TimeOfZone& timeOfZone); 84 85 } // namespace OHOS::Ace 86 87 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H 88