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#include "datetime_ex.h"
173f4cbf05Sopenharmony_ci#include <chrono>
183f4cbf05Sopenharmony_ci#include <cmath>
193f4cbf05Sopenharmony_ci#include "utils_log.h"
203f4cbf05Sopenharmony_ci
213f4cbf05Sopenharmony_ciusing namespace std::chrono;
223f4cbf05Sopenharmony_ciusing namespace std;
233f4cbf05Sopenharmony_ci
243f4cbf05Sopenharmony_cinamespace OHOS {
253f4cbf05Sopenharmony_ci
263f4cbf05Sopenharmony_ciint64_t GetSecondsSince1970ToNow()
273f4cbf05Sopenharmony_ci{
283f4cbf05Sopenharmony_ci    using secondtype = duration<int64_t>;
293f4cbf05Sopenharmony_ci    time_point<system_clock, secondtype> totalSeconds = time_point_cast<secondtype>(system_clock::now());
303f4cbf05Sopenharmony_ci    return totalSeconds.time_since_epoch().count();
313f4cbf05Sopenharmony_ci}
323f4cbf05Sopenharmony_ci
333f4cbf05Sopenharmony_ciint64_t GetSecondsSince1970ToPointTime(struct tm inputTm)
343f4cbf05Sopenharmony_ci{
353f4cbf05Sopenharmony_ci    time_t inputTime = mktime(&inputTm);
363f4cbf05Sopenharmony_ci    if (inputTime == -1) {
373f4cbf05Sopenharmony_ci        return -1;
383f4cbf05Sopenharmony_ci    }
393f4cbf05Sopenharmony_ci    system_clock::time_point pointTime = system_clock::from_time_t(inputTime);
403f4cbf05Sopenharmony_ci    using secondtype = duration<int64_t>;
413f4cbf05Sopenharmony_ci    time_point<system_clock, secondtype> totalSeconds = time_point_cast<secondtype>(pointTime);
423f4cbf05Sopenharmony_ci    return totalSeconds.time_since_epoch().count();
433f4cbf05Sopenharmony_ci}
443f4cbf05Sopenharmony_ci
453f4cbf05Sopenharmony_ciint64_t GetSecondsBetween(struct tm inputTm1, struct tm inputTm2)
463f4cbf05Sopenharmony_ci{
473f4cbf05Sopenharmony_ci    int64_t second1 = GetSecondsSince1970ToPointTime(inputTm1);
483f4cbf05Sopenharmony_ci    int64_t second2 = GetSecondsSince1970ToPointTime(inputTm2);
493f4cbf05Sopenharmony_ci    if (second1 == -1 || second2 == -1) {
503f4cbf05Sopenharmony_ci        return -1;
513f4cbf05Sopenharmony_ci    }
523f4cbf05Sopenharmony_ci    return second1 >= second2 ? (second1 - second2) : (second2 - second1);
533f4cbf05Sopenharmony_ci}
543f4cbf05Sopenharmony_ci
553f4cbf05Sopenharmony_ciint64_t GetDaysSince1970ToNow()
563f4cbf05Sopenharmony_ci{
573f4cbf05Sopenharmony_ci    typedef duration<int64_t, std::ratio<SECONDS_PER_DAY>> dayType;
583f4cbf05Sopenharmony_ci    time_point<system_clock, dayType> totalDays = time_point_cast<dayType>(system_clock::now());
593f4cbf05Sopenharmony_ci    return totalDays.time_since_epoch().count();
603f4cbf05Sopenharmony_ci}
613f4cbf05Sopenharmony_ci
623f4cbf05Sopenharmony_cibool GetSystemCurrentTime(struct tm* curTime)
633f4cbf05Sopenharmony_ci{
643f4cbf05Sopenharmony_ci    if (curTime == nullptr) {
653f4cbf05Sopenharmony_ci        return false;
663f4cbf05Sopenharmony_ci    }
673f4cbf05Sopenharmony_ci
683f4cbf05Sopenharmony_ci    auto tt = system_clock::to_time_t(system_clock::now());
693f4cbf05Sopenharmony_ci    struct tm* timeResult = nullptr;
703f4cbf05Sopenharmony_ci    timeResult = localtime_r(&tt, curTime);
713f4cbf05Sopenharmony_ci    return (timeResult != nullptr);
723f4cbf05Sopenharmony_ci}
733f4cbf05Sopenharmony_ci
743f4cbf05Sopenharmony_cibool GetLocalTimeZone(int& timezone)
753f4cbf05Sopenharmony_ci{
763f4cbf05Sopenharmony_ci    auto t1 = system_clock::to_time_t(system_clock::now());
773f4cbf05Sopenharmony_ci    auto t2 = t1;
783f4cbf05Sopenharmony_ci    struct tm curTime1 = {0};
793f4cbf05Sopenharmony_ci    struct tm curTime2 = {0};
803f4cbf05Sopenharmony_ci    struct tm* localTime = localtime_r(&t1, &curTime1);
813f4cbf05Sopenharmony_ci    struct tm* gmTime = gmtime_r(&t2, &curTime2);
823f4cbf05Sopenharmony_ci    if ((localTime == nullptr) || (gmTime == nullptr)) {
833f4cbf05Sopenharmony_ci        return false;
843f4cbf05Sopenharmony_ci    }
853f4cbf05Sopenharmony_ci
863f4cbf05Sopenharmony_ci    t1 = mktime(&curTime1);
873f4cbf05Sopenharmony_ci    t2 = mktime(&curTime2);
883f4cbf05Sopenharmony_ci    if ((t1 == -1) || (t2 == -1)) {
893f4cbf05Sopenharmony_ci        UTILS_LOGD("mktime current time failed.");
903f4cbf05Sopenharmony_ci        return false;
913f4cbf05Sopenharmony_ci    }
923f4cbf05Sopenharmony_ci
933f4cbf05Sopenharmony_ci    timezone = (static_cast<int>(t1 - t2)) / SECONDS_PER_HOUR;
943f4cbf05Sopenharmony_ci    return true;
953f4cbf05Sopenharmony_ci}
963f4cbf05Sopenharmony_ci
973f4cbf05Sopenharmony_ciint64_t GetTickCount()
983f4cbf05Sopenharmony_ci{
993f4cbf05Sopenharmony_ci    return GetMicroTickCount() / SEC_TO_MILLISEC;
1003f4cbf05Sopenharmony_ci}
1013f4cbf05Sopenharmony_ci
1023f4cbf05Sopenharmony_ciint64_t GetMicroTickCount()
1033f4cbf05Sopenharmony_ci{
1043f4cbf05Sopenharmony_ci    struct timespec ts;
1053f4cbf05Sopenharmony_ci    clock_gettime(CLOCK_MONOTONIC, &ts);
1063f4cbf05Sopenharmony_ci    return (ts.tv_sec * SEC_TO_MICROSEC + ts.tv_nsec / MICROSEC_TO_NANOSEC);
1073f4cbf05Sopenharmony_ci}
1083f4cbf05Sopenharmony_ci
1093f4cbf05Sopenharmony_ci} // OHOS
1103f4cbf05Sopenharmony_ci
111