13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci
163f4cbf05Sopenharmony_ci#ifndef DATETIME_EX_H
173f4cbf05Sopenharmony_ci#define DATETIME_EX_H
183f4cbf05Sopenharmony_ci
193f4cbf05Sopenharmony_ci#include <ctime>
203f4cbf05Sopenharmony_ci#include <cstdint>
213f4cbf05Sopenharmony_cinamespace OHOS {
223f4cbf05Sopenharmony_ci
233f4cbf05Sopenharmony_ci/**
243f4cbf05Sopenharmony_ci * Here is the definition of strct tm:
253f4cbf05Sopenharmony_ci * struct tm
263f4cbf05Sopenharmony_ci * {
273f4cbf05Sopenharmony_ci *     int tm_sec;                   // Seconds. Value range: [0-60] (1 leap second)
283f4cbf05Sopenharmony_ci *     int tm_min;                   // Minutes. Value range: [0-59]
293f4cbf05Sopenharmony_ci *     int tm_hour;                  // Hours. Value range: [0-23]
303f4cbf05Sopenharmony_ci *     int tm_mday;                  // Day. Value range: [1-31]
313f4cbf05Sopenharmony_ci *     int tm_mon;                   // Month. Value range: [0-11]
323f4cbf05Sopenharmony_ci *     int tm_year;                  // Year - 1900.
333f4cbf05Sopenharmony_ci *     int tm_wday;                  // Day of week. Value range: [0-6]
343f4cbf05Sopenharmony_ci *     int tm_yday;                  // Days in year. Value range: [0-365]
353f4cbf05Sopenharmony_ci *     int tm_isdst;                 // DST. Value range: [-1/0/1]
363f4cbf05Sopenharmony_ci *     #ifdef  __USE_BSD
373f4cbf05Sopenharmony_ci *     long int tm_gmtoff;           // Seconds east of UTC.
383f4cbf05Sopenharmony_ci *     __const char *tm_zone;        // Time zone abbreviation.
393f4cbf05Sopenharmony_ci *     #else
403f4cbf05Sopenharmony_ci *     long int __tm_gmtoff;         // Seconds east of UTC.
413f4cbf05Sopenharmony_ci *     __const char *__tm_zone;      // Time zone abbreviation.
423f4cbf05Sopenharmony_ci *     #endif
433f4cbf05Sopenharmony_ci * };
443f4cbf05Sopenharmony_ci */
453f4cbf05Sopenharmony_ci
463f4cbf05Sopenharmony_ciconstexpr int64_t SEC_TO_NANOSEC = 1000000000;
473f4cbf05Sopenharmony_ciconstexpr int64_t SEC_TO_MICROSEC = 1000000;
483f4cbf05Sopenharmony_ciconstexpr int64_t SEC_TO_MILLISEC = 1000;
493f4cbf05Sopenharmony_ciconstexpr int64_t MILLISEC_TO_NANOSEC = 1000000;
503f4cbf05Sopenharmony_ciconstexpr int64_t MICROSEC_TO_NANOSEC = 1000;
513f4cbf05Sopenharmony_ci
523f4cbf05Sopenharmony_ciconstexpr int SECONDS_PER_HOUR = 3600; // 60 * 60
533f4cbf05Sopenharmony_ciconstexpr int SECONDS_PER_DAY = 86400; // 60 * 60 * 24
543f4cbf05Sopenharmony_ci
553f4cbf05Sopenharmony_ci/**
563f4cbf05Sopenharmony_ci * @brief Converts seconds to nanoseconds.
573f4cbf05Sopenharmony_ci */
583f4cbf05Sopenharmony_ciconstexpr inline int64_t SecToNanosec(int64_t sec)
593f4cbf05Sopenharmony_ci{
603f4cbf05Sopenharmony_ci    return sec * SEC_TO_NANOSEC;
613f4cbf05Sopenharmony_ci}
623f4cbf05Sopenharmony_ci
633f4cbf05Sopenharmony_ci/**
643f4cbf05Sopenharmony_ci * @brief Converts milliseconds to nanoseconds.
653f4cbf05Sopenharmony_ci */
663f4cbf05Sopenharmony_ciconstexpr inline int64_t MillisecToNanosec(int64_t millise)
673f4cbf05Sopenharmony_ci{
683f4cbf05Sopenharmony_ci    return millise * MILLISEC_TO_NANOSEC;
693f4cbf05Sopenharmony_ci}
703f4cbf05Sopenharmony_ci
713f4cbf05Sopenharmony_ci/**
723f4cbf05Sopenharmony_ci * @brief Converts microseconds to nanoseconds.
733f4cbf05Sopenharmony_ci */
743f4cbf05Sopenharmony_ciconstexpr inline int64_t MicrosecToNanosec(int64_t microsec)
753f4cbf05Sopenharmony_ci{
763f4cbf05Sopenharmony_ci    return microsec * MICROSEC_TO_NANOSEC;
773f4cbf05Sopenharmony_ci}
783f4cbf05Sopenharmony_ci
793f4cbf05Sopenharmony_ci/**
803f4cbf05Sopenharmony_ci * @brief Converts nanoseconds to seconds.
813f4cbf05Sopenharmony_ci */
823f4cbf05Sopenharmony_ciconstexpr inline int64_t NanosecToSec(int64_t nanosec)
833f4cbf05Sopenharmony_ci{
843f4cbf05Sopenharmony_ci    return nanosec / SEC_TO_NANOSEC;
853f4cbf05Sopenharmony_ci}
863f4cbf05Sopenharmony_ci
873f4cbf05Sopenharmony_ci/**
883f4cbf05Sopenharmony_ci * @brief Convert nanoseconds to milliseconds.
893f4cbf05Sopenharmony_ci */
903f4cbf05Sopenharmony_ciconstexpr inline int64_t NanosecToMillisec(int64_t nanosec)
913f4cbf05Sopenharmony_ci{
923f4cbf05Sopenharmony_ci    return nanosec / MILLISEC_TO_NANOSEC;
933f4cbf05Sopenharmony_ci}
943f4cbf05Sopenharmony_ci
953f4cbf05Sopenharmony_ci/**
963f4cbf05Sopenharmony_ci * @brief Converts nanoseconds to microseconds.
973f4cbf05Sopenharmony_ci */
983f4cbf05Sopenharmony_ciconstexpr inline int64_t NanosecToMicrosec(int64_t nanosec)
993f4cbf05Sopenharmony_ci{
1003f4cbf05Sopenharmony_ci    return nanosec / MICROSEC_TO_NANOSEC;
1013f4cbf05Sopenharmony_ci}
1023f4cbf05Sopenharmony_ci
1033f4cbf05Sopenharmony_ci/**
1043f4cbf05Sopenharmony_ci * @brief Obtains the number of seconds from 00:00:00 on January 1, 1970
1053f4cbf05Sopenharmony_ci * to the current time.
1063f4cbf05Sopenharmony_ci */
1073f4cbf05Sopenharmony_ciint64_t GetSecondsSince1970ToNow();
1083f4cbf05Sopenharmony_ci
1093f4cbf05Sopenharmony_ci/**
1103f4cbf05Sopenharmony_ci * @brief Obtains the number of seconds from 00:00:00 on January 1, 1970
1113f4cbf05Sopenharmony_ci * to the specified point of time.
1123f4cbf05Sopenharmony_ci */
1133f4cbf05Sopenharmony_ciint64_t GetSecondsSince1970ToPointTime(struct tm inputTm);
1143f4cbf05Sopenharmony_ci
1153f4cbf05Sopenharmony_ci/**
1163f4cbf05Sopenharmony_ci * @brief Obtains the number of seconds between inputTm1 and inputTm2.
1173f4cbf05Sopenharmony_ci */
1183f4cbf05Sopenharmony_ciint64_t GetSecondsBetween(struct tm inputTm1, struct tm inputTm2);
1193f4cbf05Sopenharmony_ci
1203f4cbf05Sopenharmony_ci/**
1213f4cbf05Sopenharmony_ci * @brief Obtains the number of days from January 1, 1970 to the current date.
1223f4cbf05Sopenharmony_ci */
1233f4cbf05Sopenharmony_ciint64_t GetDaysSince1970ToNow();
1243f4cbf05Sopenharmony_ci
1253f4cbf05Sopenharmony_ci/**
1263f4cbf05Sopenharmony_ci * @brief Obtains the local time zone.
1273f4cbf05Sopenharmony_ci *
1283f4cbf05Sopenharmony_ci * @param timezone Indicates the time zone. A total of 24 time zones are
1293f4cbf05Sopenharmony_ci * supported, with the eastern time zones represented by +1 to +12, and
1303f4cbf05Sopenharmony_ci * the western time zones -1 to -12.
1313f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the operation is successful;
1323f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
1333f4cbf05Sopenharmony_ci */
1343f4cbf05Sopenharmony_cibool GetLocalTimeZone(int& timezone);
1353f4cbf05Sopenharmony_ci
1363f4cbf05Sopenharmony_ci/**
1373f4cbf05Sopenharmony_ci * @brief Obtains the current time.
1383f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the operation is successful;
1393f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise.
1403f4cbf05Sopenharmony_ci */
1413f4cbf05Sopenharmony_cibool GetSystemCurrentTime(struct tm* curTime);
1423f4cbf05Sopenharmony_ci
1433f4cbf05Sopenharmony_ci/**
1443f4cbf05Sopenharmony_ci * @brief Obtains the number of milliseconds since the system was started.
1453f4cbf05Sopenharmony_ci */
1463f4cbf05Sopenharmony_ciint64_t GetTickCount();
1473f4cbf05Sopenharmony_ci
1483f4cbf05Sopenharmony_ci/**
1493f4cbf05Sopenharmony_ci * @brief Obtains the number of microseconds since the system was started.
1503f4cbf05Sopenharmony_ci */
1513f4cbf05Sopenharmony_ciint64_t GetMicroTickCount();
1523f4cbf05Sopenharmony_ci}
1533f4cbf05Sopenharmony_ci
1543f4cbf05Sopenharmony_ci#endif
155