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