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