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#include "datetime_ex.h"
17#include <chrono>
18#include <cmath>
19#include "utils_log.h"
20
21using namespace std::chrono;
22using namespace std;
23
24namespace OHOS {
25
26int64_t GetSecondsSince1970ToNow()
27{
28    using secondtype = duration<int64_t>;
29    time_point<system_clock, secondtype> totalSeconds = time_point_cast<secondtype>(system_clock::now());
30    return totalSeconds.time_since_epoch().count();
31}
32
33int64_t GetSecondsSince1970ToPointTime(struct tm inputTm)
34{
35    time_t inputTime = mktime(&inputTm);
36    if (inputTime == -1) {
37        return -1;
38    }
39    system_clock::time_point pointTime = system_clock::from_time_t(inputTime);
40    using secondtype = duration<int64_t>;
41    time_point<system_clock, secondtype> totalSeconds = time_point_cast<secondtype>(pointTime);
42    return totalSeconds.time_since_epoch().count();
43}
44
45int64_t GetSecondsBetween(struct tm inputTm1, struct tm inputTm2)
46{
47    int64_t second1 = GetSecondsSince1970ToPointTime(inputTm1);
48    int64_t second2 = GetSecondsSince1970ToPointTime(inputTm2);
49    if (second1 == -1 || second2 == -1) {
50        return -1;
51    }
52    return second1 >= second2 ? (second1 - second2) : (second2 - second1);
53}
54
55int64_t GetDaysSince1970ToNow()
56{
57    typedef duration<int64_t, std::ratio<SECONDS_PER_DAY>> dayType;
58    time_point<system_clock, dayType> totalDays = time_point_cast<dayType>(system_clock::now());
59    return totalDays.time_since_epoch().count();
60}
61
62bool GetSystemCurrentTime(struct tm* curTime)
63{
64    if (curTime == nullptr) {
65        return false;
66    }
67
68    auto tt = system_clock::to_time_t(system_clock::now());
69    struct tm* timeResult = nullptr;
70    timeResult = localtime_r(&tt, curTime);
71    return (timeResult != nullptr);
72}
73
74bool GetLocalTimeZone(int& timezone)
75{
76    auto t1 = system_clock::to_time_t(system_clock::now());
77    auto t2 = t1;
78    struct tm curTime1 = {0};
79    struct tm curTime2 = {0};
80    struct tm* localTime = localtime_r(&t1, &curTime1);
81    struct tm* gmTime = gmtime_r(&t2, &curTime2);
82    if ((localTime == nullptr) || (gmTime == nullptr)) {
83        return false;
84    }
85
86    t1 = mktime(&curTime1);
87    t2 = mktime(&curTime2);
88    if ((t1 == -1) || (t2 == -1)) {
89        UTILS_LOGD("mktime current time failed.");
90        return false;
91    }
92
93    timezone = (static_cast<int>(t1 - t2)) / SECONDS_PER_HOUR;
94    return true;
95}
96
97int64_t GetTickCount()
98{
99    return GetMicroTickCount() / SEC_TO_MILLISEC;
100}
101
102int64_t GetMicroTickCount()
103{
104    struct timespec ts;
105    clock_gettime(CLOCK_MONOTONIC, &ts);
106    return (ts.tv_sec * SEC_TO_MICROSEC + ts.tv_nsec / MICROSEC_TO_NANOSEC);
107}
108
109} // OHOS
110
111