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_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 std::string ConvertTimestampToStr(int64_t timestamp); 42 43 struct TimeOfNow final { hoursWest_final44 explicit TimeOfNow(double hoursWest = INT_MAX) : hoursWest_(hoursWest) {} 45 ~TimeOfNow() = default; 46 47 // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 48 // Valid range of [hoursWest] is [-14, 12]. Set default value to DBL_MAX to use current time zone by default. 49 int32_t hoursWest_ = INT_MAX; 50 int32_t second_ = 0; 51 int32_t minute_ = 0; 52 int32_t hour12_ = 0; // 12-hour clock 53 int32_t hour24_ = 0; // 24-hour clock 54 int64_t timeUsec_ = 0L; // microsecond. 1 second = 1000 millisecond = 1000000 microsecond 55 }; 56 57 bool IsHoursWestValid(int32_t& hoursWest); 58 59 TimeOfNow GetTimeOfNow(int32_t hoursWest = INT_MAX); 60 61 bool IsDayTime(const TimeOfNow& timeOfNow); 62 63 struct TimeOfZone final { hoursWest_final64 explicit TimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST) : hoursWest_(hoursWest) {} 65 ~TimeOfZone() = default; 66 67 // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 68 // Valid range of [hoursWest] is [-14, 12]. Set default value to DEFAULT_HOURS_WEST to use current time zone by default. 69 int32_t hoursWest_ = DEFAULT_HOURS_WEST; 70 int32_t second_ = 0; 71 int32_t minute_ = 0; 72 int32_t hour12_ = 0; // 12-hour clock 73 int32_t hour24_ = 0; // 24-hour clock 74 int64_t timeUsec_ = 0L; // microsecond. 1 second = 1000 millisecond = 1000000 microsecond 75 }; 76 77 bool HoursWestIsValid(int32_t& hoursWest); 78 79 TimeOfZone GetTimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST); 80 81 bool IsDayTime(const TimeOfZone& timeOfZone); 82 83 } // namespace OHOS::Ace 84 85 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H 86