123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H 1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_ci#include <cstdint> 2023b3eb3cSopenharmony_ci#include <ctime> 2123b3eb3cSopenharmony_ci#include <float.h> 2223b3eb3cSopenharmony_ci#include <limits.h> 2323b3eb3cSopenharmony_ci#include <string> 2423b3eb3cSopenharmony_ci 2523b3eb3cSopenharmony_ci#include "base/utils/macros.h" 2623b3eb3cSopenharmony_ci 2723b3eb3cSopenharmony_cinamespace OHOS::Ace { 2823b3eb3cSopenharmony_ciconstexpr int32_t DEFAULT_HOURS_WEST = -8; 2923b3eb3cSopenharmony_ci 3023b3eb3cSopenharmony_ci/** 3123b3eb3cSopenharmony_ci* The GetMicroTickCount function get current microseconds since the system was started. 3223b3eb3cSopenharmony_ci*/ 3323b3eb3cSopenharmony_ciACE_FORCE_EXPORT int64_t GetMicroTickCount(); 3423b3eb3cSopenharmony_ci 3523b3eb3cSopenharmony_ciint64_t GetSysTimestamp(); 3623b3eb3cSopenharmony_ci 3723b3eb3cSopenharmony_ci/** 3823b3eb3cSopenharmony_ci* return milliseconds to 1970-1-1 0:0:0 3923b3eb3cSopenharmony_ci*/ 4023b3eb3cSopenharmony_ciint64_t GetCurrentTimestamp(); 4123b3eb3cSopenharmony_ciint64_t GetCurrentTimestampMicroSecond(); 4223b3eb3cSopenharmony_cistd::string ConvertTimestampToStr(int64_t timestamp); 4323b3eb3cSopenharmony_ci 4423b3eb3cSopenharmony_cistruct TimeOfNow final { 4523b3eb3cSopenharmony_ci explicit TimeOfNow(double hoursWest = INT_MAX) : hoursWest_(hoursWest) {} 4623b3eb3cSopenharmony_ci ~TimeOfNow() = default; 4723b3eb3cSopenharmony_ci 4823b3eb3cSopenharmony_ci // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 4923b3eb3cSopenharmony_ci // Valid range of [hoursWest] is [-14, 12]. Set default value to DBL_MAX to use current time zone by default. 5023b3eb3cSopenharmony_ci int32_t hoursWest_ = INT_MAX; 5123b3eb3cSopenharmony_ci int32_t second_ = 0; 5223b3eb3cSopenharmony_ci int32_t minute_ = 0; 5323b3eb3cSopenharmony_ci int32_t hour12_ = 0; // 12-hour clock 5423b3eb3cSopenharmony_ci int32_t hour24_ = 0; // 24-hour clock 5523b3eb3cSopenharmony_ci int64_t timeUsec_ = 0L; // microsecond. 1 second = 1000 millisecond = 1000000 microsecond 5623b3eb3cSopenharmony_ci}; 5723b3eb3cSopenharmony_ci 5823b3eb3cSopenharmony_cibool IsHoursWestValid(int32_t& hoursWest); 5923b3eb3cSopenharmony_ci 6023b3eb3cSopenharmony_ciTimeOfNow GetTimeOfNow(int32_t hoursWest = INT_MAX); 6123b3eb3cSopenharmony_ci 6223b3eb3cSopenharmony_cibool IsDayTime(const TimeOfNow& timeOfNow); 6323b3eb3cSopenharmony_ci 6423b3eb3cSopenharmony_cistruct TimeOfZone final { 6523b3eb3cSopenharmony_ci explicit TimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST) : hoursWest_(hoursWest) {} 6623b3eb3cSopenharmony_ci ~TimeOfZone() = default; 6723b3eb3cSopenharmony_ci 6823b3eb3cSopenharmony_ci // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 6923b3eb3cSopenharmony_ci // Valid range of [hoursWest] is [-14, 12]. 7023b3eb3cSopenharmony_ci // Set default value to DEFAULT_HOURS_WEST to use current time zone by default. 7123b3eb3cSopenharmony_ci int32_t hoursWest_ = DEFAULT_HOURS_WEST; 7223b3eb3cSopenharmony_ci int32_t second_ = 0; 7323b3eb3cSopenharmony_ci int32_t minute_ = 0; 7423b3eb3cSopenharmony_ci int32_t hour12_ = 0; // 12-hour clock 7523b3eb3cSopenharmony_ci int32_t hour24_ = 0; // 24-hour clock 7623b3eb3cSopenharmony_ci int64_t timeUsec_ = 0L; // microsecond. 1 second = 1000 millisecond = 1000000 microsecond 7723b3eb3cSopenharmony_ci}; 7823b3eb3cSopenharmony_ci 7923b3eb3cSopenharmony_cibool HoursWestIsValid(int32_t& hoursWest); 8023b3eb3cSopenharmony_ci 8123b3eb3cSopenharmony_ciTimeOfZone GetTimeOfZone(int32_t hoursWest = DEFAULT_HOURS_WEST); 8223b3eb3cSopenharmony_ci 8323b3eb3cSopenharmony_cibool IsDayTime(const TimeOfZone& timeOfZone); 8423b3eb3cSopenharmony_ci 8523b3eb3cSopenharmony_ci} // namespace OHOS::Ace 8623b3eb3cSopenharmony_ci 8723b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_TIME_UTIL_H 88