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