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#ifndef DATETIME_EX_H 17#define DATETIME_EX_H 18 19#include <ctime> 20#include <cstdint> 21namespace OHOS { 22 23/** 24 * Here is the definition of strct tm: 25 * struct tm 26 * { 27 * int tm_sec; // Seconds. Value range: [0-60] (1 leap second) 28 * int tm_min; // Minutes. Value range: [0-59] 29 * int tm_hour; // Hours. Value range: [0-23] 30 * int tm_mday; // Day. Value range: [1-31] 31 * int tm_mon; // Month. Value range: [0-11] 32 * int tm_year; // Year - 1900. 33 * int tm_wday; // Day of week. Value range: [0-6] 34 * int tm_yday; // Days in year. Value range: [0-365] 35 * int tm_isdst; // DST. Value range: [-1/0/1] 36 * #ifdef __USE_BSD 37 * long int tm_gmtoff; // Seconds east of UTC. 38 * __const char *tm_zone; // Time zone abbreviation. 39 * #else 40 * long int __tm_gmtoff; // Seconds east of UTC. 41 * __const char *__tm_zone; // Time zone abbreviation. 42 * #endif 43 * }; 44 */ 45 46constexpr int64_t SEC_TO_NANOSEC = 1000000000; 47constexpr int64_t SEC_TO_MICROSEC = 1000000; 48constexpr int64_t SEC_TO_MILLISEC = 1000; 49constexpr int64_t MILLISEC_TO_NANOSEC = 1000000; 50constexpr int64_t MICROSEC_TO_NANOSEC = 1000; 51 52constexpr int SECONDS_PER_HOUR = 3600; // 60 * 60 53constexpr int SECONDS_PER_DAY = 86400; // 60 * 60 * 24 54 55/** 56 * @brief Converts seconds to nanoseconds. 57 */ 58constexpr inline int64_t SecToNanosec(int64_t sec) 59{ 60 return sec * SEC_TO_NANOSEC; 61} 62 63/** 64 * @brief Converts milliseconds to nanoseconds. 65 */ 66constexpr inline int64_t MillisecToNanosec(int64_t millise) 67{ 68 return millise * MILLISEC_TO_NANOSEC; 69} 70 71/** 72 * @brief Converts microseconds to nanoseconds. 73 */ 74constexpr inline int64_t MicrosecToNanosec(int64_t microsec) 75{ 76 return microsec * MICROSEC_TO_NANOSEC; 77} 78 79/** 80 * @brief Converts nanoseconds to seconds. 81 */ 82constexpr inline int64_t NanosecToSec(int64_t nanosec) 83{ 84 return nanosec / SEC_TO_NANOSEC; 85} 86 87/** 88 * @brief Convert nanoseconds to milliseconds. 89 */ 90constexpr inline int64_t NanosecToMillisec(int64_t nanosec) 91{ 92 return nanosec / MILLISEC_TO_NANOSEC; 93} 94 95/** 96 * @brief Converts nanoseconds to microseconds. 97 */ 98constexpr inline int64_t NanosecToMicrosec(int64_t nanosec) 99{ 100 return nanosec / MICROSEC_TO_NANOSEC; 101} 102 103/** 104 * @brief Obtains the number of seconds from 00:00:00 on January 1, 1970 105 * to the current time. 106 */ 107int64_t GetSecondsSince1970ToNow(); 108 109/** 110 * @brief Obtains the number of seconds from 00:00:00 on January 1, 1970 111 * to the specified point of time. 112 */ 113int64_t GetSecondsSince1970ToPointTime(struct tm inputTm); 114 115/** 116 * @brief Obtains the number of seconds between inputTm1 and inputTm2. 117 */ 118int64_t GetSecondsBetween(struct tm inputTm1, struct tm inputTm2); 119 120/** 121 * @brief Obtains the number of days from January 1, 1970 to the current date. 122 */ 123int64_t GetDaysSince1970ToNow(); 124 125/** 126 * @brief Obtains the local time zone. 127 * 128 * @param timezone Indicates the time zone. A total of 24 time zones are 129 * supported, with the eastern time zones represented by +1 to +12, and 130 * the western time zones -1 to -12. 131 * @return Returns <b>true</b> if the operation is successful; 132 * returns <b>false</b> otherwise. 133 */ 134bool GetLocalTimeZone(int& timezone); 135 136/** 137 * @brief Obtains the current time. 138 * @return Returns <b>true</b> if the operation is successful; 139 * returns <b>false</b> otherwise. 140 */ 141bool GetSystemCurrentTime(struct tm* curTime); 142 143/** 144 * @brief Obtains the number of milliseconds since the system was started. 145 */ 146int64_t GetTickCount(); 147 148/** 149 * @brief Obtains the number of microseconds since the system was started. 150 */ 151int64_t GetMicroTickCount(); 152} 153 154#endif 155